1
- import _thread
2
1
import json
3
2
import socket
4
3
import uuid
4
+ from threading import Thread
5
5
6
6
import byte_utils
7
7
8
8
9
9
class SocketServer :
10
-
11
- def __init__ (self , ip , port , motd , version_text , kick_message , samples , server_icon , logger , show_ip ):
10
+ def __init__ (self , ip , port , motd , version_text , kick_message , samples , server_icon , logger , show_hostname ):
12
11
self .sock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
13
12
self .ip = ip
14
13
self .port = port
@@ -18,7 +17,7 @@ def __init__(self, ip, port, motd, version_text, kick_message, samples, server_i
18
17
self .samples = samples
19
18
self .server_icon = server_icon
20
19
self .logger = logger
21
- self .show_ip = show_ip
20
+ self .show_hostname = show_hostname
22
21
23
22
def on_new_client (self , client_socket , addr ):
24
23
data = client_socket .recv (1024 )
@@ -42,8 +41,8 @@ def on_new_client(self, client_socket, addr):
42
41
(port , i ) = byte_utils .read_ushort (data , i )
43
42
(state , i ) = byte_utils .read_varint (data , i )
44
43
45
- fqdn = socket .getfqdn (addr [ 0 ] )
46
- if self .show_ip and client_ip != fqdn :
44
+ fqdn = socket .getfqdn (client_ip )
45
+ if self .show_hostname and client_ip != fqdn :
47
46
client_ip = fqdn + "/" + client_ip
48
47
49
48
if state == 1 :
@@ -112,7 +111,7 @@ def start(self):
112
111
self .logger .info ("Server started on %s:%s! Waiting for incoming connections..." % (self .ip , self .port ))
113
112
while 1 :
114
113
(client , address ) = self .sock .accept ()
115
- _thread . start_new_thread ( self .on_new_client , (client , address ,))
114
+ Thread ( target = self .on_new_client , daemon = True , args = (client , address ,)). start ( )
116
115
117
116
def close (self ):
118
117
self .sock .close ()
0 commit comments