python实现socket编程
实现的主要过程是:
tcp server:
import socket
import sys
host = "127.0.0.1"
port = 8080
try :
sersock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #建立socket
sersock.bind((host,port)) #绑定端口
sersock.listen(10) #设置监听
except:
sys.exit(1)
while True:
conn,addr = sersock.accept() #接受链接信号
recvMsg = conn.recv(1024) #接受客户端的信号
sendMsg = " " #设置发送消息
conn.send(sendMsg) #发送消息
conn.close()
tcp client
import sys
import socket
host = "127.0.0.1"
port = 8080
try:
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect((host,port)) #建立链接
except:
sys.exit(1)
sendMsg = " " #填写发送的消息
client.send(sendMsg)
recvMsg = client.recv(1024) #接受服务端发送的消息
socket函数.png函数 的实现原型:
connect'通信函数.png
网友评论