今日内容
基于TCP协议编写的套接字
一:套接字通信(socket)模块
1.1:服务端
import socket
#1、买手机
phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#2、绑定电话卡
phone.bind(("127.0.0.1",8080)) #ip地址和端口号
#3、开机
phone.listen(5) #小整数池设为5
#4、接收链接请求
while True:
conn,client_addr = phone.accept()
# print(client_addr)
#5、接发消息
while True:
data = conn.recv(1024)
print(data.decode("utf-8"))
msg = input(">>>>:")
conn.send(msg.encode("utf_8"))
#6、挂电话
conn.close()
#7、关机
phone.close()
1.2:客户端
import socket
#1、买手机
phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#2、打电话
phone.connect(("127.0.0.1",8080))
#3、发收数据
while True:
msg = input(">>>>>: ").strip()
phone.send(msg.encode("utf-8"))
data = phone.recv(1024)
print(data.decode("utf-8"))
#4、关机
phone.close()