Python3网络编程
# 导入socket、sys模块
import socket
import sys
# 创建socket对象
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 获取本地主机名
host = socket.gethostname()
port = 9999
# 绑定端口
serversocket.bind((host, port))
# 设置最大连接数,超过后排队
serversocket.listen(5)
while True:
# 建立客户端连接
clientsocket, addr = serversocket.accept()
print("连接地址:%s" % str(addr))
msg = '欢迎访问金泰祥搬家!' + "\r\n"
clientsocket.send(msg.encode('utf-8'))
clientsocket.close()
客户端
# 文件名:client.py
# 导入socket、sys模块
import socket
import sys
# 创建socket对象
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#获取本地主机名
host = socket.gethostname()
#设置端口
port = 9999
# 连接服务,指定主机和端口
s.connect((host,port))
# 接收小鱼1024字节的数据
msg = s.recv(1024)
s.close()
print(msg.decode('utf-8'))
网友评论