创建TCP服务器和TCP客户端

时间:2022-05-10 15:14:29
import socket
host='127.0.0.1'
port=8080
web=socket.socket()
web.bind((host,port))
web.listen(5)# 设置最多连接数
print('服务器等待客户端连接')
#开启死循环
while True:
coon,addr=web.accept()# 建立客户端连接
data=coon.recv(1024).decode()# 获取客户端请求数据
print(data)
coon.sendall(b'HTTP/1.1 200 ok\r\n\r\nhello world')
coon.close()

-------------------------------------------------------------------------------------------

import socket
s=socket.socket()
host='127.0.0.1'
port=8080
s.connect((host,port))
send_data=input('请输入要发送的数据:')
s.send(send_data.encode())
recvData=s.recv(1024).decode()
print('接收到的数据为:',recvData)
s.close()

创建TCP服务器和TCP客户端

创建TCP服务器和TCP客户端