标签:cli 最大消息 创建 实例化 utf-8 socket lis sock client
服务端
import socket
server = socket.socket() #创建一个socket实例化对象(创建了一部手机)
ip_port = (‘192.168.12.46‘,8001)#电话卡
server.bind(ip_port)#绑定IP地址和端口(插卡)
server.listen()#监听(开机)
conn,addr = server.accept()#等待连接(电话线路)
while 1:
server_msg = input(‘wulaoban>>‘)#输入要发送给客户端的消息
server_msg = server_msg.encode(‘utf-8‘)#吧输入的消息转换成utf-8
conn.send(server_msg)#发送消息
from_client_msg = conn.recv(1024)#一次性接受最大消息长度1024
from_client_msg = from_client_msg.decode(‘utf-8‘)#把接受得到的utf-8型的消息解码
print(from_client_msg)
conn.close()
server.close()
客户端
import socket
client = socket.socket()
server_ip_prot = (‘192.168.12.46‘,8001)#服务端ip地址和端口)
client.connect(server_ip_prot)#连接服务端的应用程序1
while 1:
from_server_msg = client.recv(1024)
from_server_msg = from_server_msg.decode(‘utf-8‘)
print(‘来自服务端的消息‘,from_server_msg)
print(from_server_msg)
client_msg = input(‘客户端>>‘)
client_msg = client_msg.encode(‘utf-8‘)
client.send(client_msg)
标签:cli 最大消息 创建 实例化 utf-8 socket lis sock client
原文地址:https://www.cnblogs.com/wwjx/p/10045631.html