首页 > 其他 > 详细

创建TCP服务器和TCP客户端

时间:2019-12-18 19:55:20      阅读:100      评论:0      收藏:0      [点我收藏+]
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客户端

原文:https://www.cnblogs.com/startl/p/12061446.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!