100 精币
import json
import threading
import socketserver
class ReceiveMsgSocketServer (socketserver .BaseRequestHandler ):
def __init__ (self , *args , **kwargs ):
super ().__init__ (*args , **kwargs )
def handle (self ):
conn = self .request
while True :
try :
ptr_data = b ""
while True :
data = conn .recv(1024 )
ptr_data += data
if len (data ) == 0 or data [-1 ] == 0x A :
break
msg = json .loads (ptr_data )
ReceiveMsgSocketServer .msg_callback (msg )
except OSError :
break
except json .JSONDecodeError :
pass
conn .sendall("200 OK" .encode ())
conn .close()
@ staticmethod
def msg_callback (msg ):
print (msg )
def start_socket_server (port : int = 19099 ,
request_handler =ReceiveMsgSocketServer ,
main_thread : bool = True ) -> int or None :
ip_port = ("127.0.0.1" , port )
try :
s = socketserver .ThreadingTCPServer (ip_port , request_handler )
if main_thread :
s .serve_forever ()
else :
socket_server = threading .Thread (target =s .serve_forever )
socket_server .setDaemon (True )
socket_server .start ()
return socket_server .ident
except KeyboardInterrupt :
pass
except Exception as e :
print (e )
return None
if __name__ == '__main__' :
start_socket_server ()
我来回答
求助知识: 请将问题描述清楚,最好把你有问题的源码打包上传上来,这样更方便大家帮助你。
友情提醒: 本版被采纳的主题可在
申请荣誉值 帖子申请荣誉值,获得
3点 荣誉值,荣誉值可兑换荣誉会员、终身vip用户组。