1 #!/usr/bin/python 2 # -*- coding: UTF-8 -*- 3 # 文件名:tcpserver.py 4 5 import socket 6 import time 7 MaxBytes=1024*1024 8 9 server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 10 server.settimeout(600) 11 host = '172.28.71.82' 12 #host = socket.gethostname() 13 port = 11223 14 server.bind((host, port)) # 绑定端口 15 16 server.listen(10) # 监听 17 try: 18 while True: 19 client,addr = server.accept() # 等待客户端连接 20 print(addr," 连接上了") 21 while True: 22 data = client.recv(MaxBytes) 23 if not data: 24 print('数据为空,我要退出了') 25 break 26 localTime = time.asctime( time.localtime(time.time())) 27 print(localTime,' 接收到数据字节数:',len(data)) 28 print(data.decode()) 29 client.sendall(data) 30 print("发送完毕") 31 except BaseException as e: 32 print("出现异常:") 33 print(repr(e)) 34 finally: 35 server.close() # 关闭连接 36 print("我已经退出了,后会无期")
标签:socket,time,tcp,server,host,简单,print,data,服务端 From: https://www.cnblogs.com/jlh-/p/16779926.html