部署服务器:
(部署服务端) 获得阿里云虚拟机,连接x-shell,将代码部署上去
import socket
phine=socket.socket(socket.AF_INET,socket.SOCK_STREAM)#流式协议=》tcp协议
#phone=socket.socket(socket.AF_INET,socket.SOCK_DGGRAN)#数据报协议=》udp
#本地回环地址(127.0.0.1)
phone.bind(('127.0.0.1',8988))(连接时要修改IP地址)
phone.listen(5)
print("服务端开启")
while True:
conn,client_ader=phine.accept()
while True:
try:
data=conn.recv(1024)
conn.send(data.upper())
except Exception:
break
conn.close()
phine.close()
用客户端连接
import socket
phone = socket.socket()
phone.connect(('127.0.0.1', 8988))
while True:
msg = input('输入').strip() #msg=""
if ien(msg) == 0:
phone.send(msg.encode('utf-8'))
data = phone.recv(1024)
print(data.decode("utf-8"))
#添加可进入的端口