python(socket通信实例单用户)

client客户端:

# Author:Sooele

#client
import socket

client = socket.socket()   #声明socket协议类型,同时生成socket链接对象
client = socket.connect(('Sooele',6969))

client.send(b"Hello World")
data = client.recv()

client.close()

 

sever服务端:

# Author:Sooele

#server

import socket
server = socket.socket()
server.bind(('Sooele',6969))# 绑定需要监听的端口
server.listen() #监听

print("等待数据。。。。。。")
conn,addr = server.accept() #等待 conn是链接标记类 addr是对方的ip地址
#conn就是客户端连过来而在服务器端为其生成的链接实例
print(conn,addr)

print("数据来了。。。。")
data = conn.recv(1024)
print("recv:",data)
conn.send((data.upper))

server.close()

发表评论