【发布时间】:2017-12-10 14:20:20
【问题描述】:
有没有办法在客户端连接到该服务器之前停止 while 循环? 服务器应该为每个新的客户端连接创建一个新线程,有可能吗?
import socket
import threading
def clientdialog(myS):
conn, addr = myS.accept()
print ("Connection from: " + str(addr))
while 1:
data = conn.recv(1024).decode("utf-8")
if not data or data == 'q':
break
print ("from connected user: " + str(data))
host = "192.168.1.3"
port = 1998
mySocket = socket.socket()
mySocket.bind((host,port))
while True:
mySocket.listen(10)
#whait until socket don't connect
try:
threading._start_new_thread(clientdialog, (mySocket))
except:
print("error starting thread")
【问题讨论】:
标签: python multithreading python-3.x sockets python-multithreading