tcp多线程服务器 from threading import Thread
from socket import *
def rData(newSocket):
try:
while True:
newData = newSocket.recv(1024)
if len(newData) > 0:
print("%s"%newData)
else:
print("关闭套接字....")
break
finally:
newSocket.close()
def main():
tSocket = socket(AF_INET,SOCK_STREAM)
tSocket.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
bindAddr = ("",7288)
tSocket.bind(bindAddr)
tSocket.listen(5)
try:
while True:
newSocket,ipData = tSocket.accept()
t = Thread(target=rData,args=(newSocket,))
t.start()
finally:
tSocket.close()
if __name__ == "__main__":
main()