Skip to content

Commit 7403a16

Browse files
committed
Server shows local IP
1 parent 5c77dd1 commit 7403a16

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

SnakeServer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@
88
from PyQt5.QtCore import Qt
99

1010
def get_ip():
11-
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
1211
try:
13-
s.connect(('0.0.0.0', 1))
14-
IP = s.getsockname()[0]
12+
h_name = socket.gethostname()
13+
IP = socket.gethostbyname(h_name)
1514
except Exception:
1615
IP = '127.0.0.1'
17-
finally:
18-
s.close()
1916
return IP
2017

2118
class SnakeServer:
@@ -113,6 +110,9 @@ def __init__(self, *args, **kwargs):
113110
self.server_status.resize(150, 30)
114111
self.server_status.move(0, 0)
115112

113+
ip_label = QLabel("Local IP:" + get_ip(),self)
114+
ip_label.move(200,0)
115+
116116
start_btn = QPushButton("Start", self)
117117
start_btn.setToolTip("Start your Server")
118118
start_btn.resize(150, 100)

0 commit comments

Comments
 (0)