# 导入Socket库及依赖库import socket
import threading
import time
# 建立TCP连接
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
1.2绑定监听的地址和端口
# 绑定地址及监听端口
s.bind(('127.0.0.1',6666))
1.3监听端口
# 调用listen方法监听端口
s.listen(5)print('Wait for connection...')
1.4创建服务器端应答函数
# 创建服务器端应答函数deftcp(sock, addr):print('Accept new connecton from %s:%s...'% addr)
sock.send(b'Success!')whileTrue:
data=sock.recv(1024)
time.sleep(1)ifnot data or data.decode('utf-8')=='exit':break
sock.send(('Welcome! %s!'% data.decode('utf-8')).encode('utf-8'))
sock.close()print('Connection from %s:%s closed.'% addr)