TCP通信

2024-01-10 16:12:13

tcp是一种可靠的网络传输协议,python可以实现该通信

简单的通信

服务端代码

from socket import *

server_socket=socket(AF_INET,SOCK_STREAM)

server_socket.bind(("127.0.0.1",8888))
server_socket.listen(5)    #监听数量
print("服务器已启动")

client_socket,client_addr=server_socket.accept()
data=client_socket.recv(1024)
print("客户端发送来的数据为:",data.decode('utf-8'))    #解码数据
client_socket.send("我是服务端".encode("utf-8"))	#向客户端发送数据

server_socket.close()

客户端代码

from socket import *

client_socket=socket()

client_socket.connect(("127.0.0.1",8888))
print("与服务器连接成功")
client_socket.send("我是客户端".encode('utf-8'))    #编码

data=client_socket.recv(1024)	#接收服务端的数据
print(data.decode("utf-8"))    #解码
client_socket.close()

多次通信

可以通过循环的方式实现多次消息的传递

服务端

#服务端:当输入bye时,结束通信
from socket import *

server_socket=socket(AF_INET,SOCK_STREAM)

server_socket.bind(("127.0.0.1",8888))
server_socket.listen(5)
print("服务器已启动"+"-"*100)

client_socket,client_addr=server_socket.accept()

data=client_socket.recv(1024)
data=data.decode("utf-8")
while data!="bye":
    print(data)
    data=input("请输入内容:")
    if data=="bye":
        break
    client_socket.send(data.encode("utf-8"))
    data=client_socket.recv(1024).decode("utf-8")

client_socket.close()
server_socket.close()
print("服务器结束"+"-"*100)

客户端

#客户端
from socket import *

client_socket=socket()
client_socket.connect(("127.0.0.1",8888))
print("与服务器连接成功"+"-"*100)

data=""
while data!="bye":
    data=input("请输入内容")
    client_socket.send(data.encode("utf-8"))
    if data == "bye":
        break
    data=client_socket.recv(1024).decode("utf-8")
    print(data)
client_socket.close()

文件的网络传输

文本文件的传输案例

#服务端
from socket import *

server_socket=socket(AF_INET,SOCK_STREAM)

server_socket.bind(("127.0.0.1",8888))
server_socket.listen(5)
print("服务器已启动"+"-"*100)

client_socket,client_addr=server_socket.accept()

print("开始传输文件")
with open("test.CSV",'r',encoding="utf-8") as file:
    bytes=file.read(10)
    
    while bytes:
        bytes=bytes.encode("utf-8") #编码
        client_socket.send(bytes)
        bytes=file.read(10)
    
    
print("传输结束")
client_socket.close()
server_socket.close()
print("服务器结束"+"-"*100)


#客户端
from socket import *

client_socket=socket()
client_socket.connect(("127.0.0.1",8888))
print("与服务器连接成功"+"-"*100)

with open("test.csv",'w',encoding='utf-8') as file:
    data=client_socket.recv(10).decode("utf-8") #解码
    while data:
        file.write(data)
        data=client_socket.recv(10).decode("utf-8")
print("阅读完成")
client_socket.close()

二进制文件的传输案例

#服务端
from socket import *

server_socket=socket(AF_INET,SOCK_STREAM)

server_socket.bind(("127.0.0.1",8888))
server_socket.listen(5)
print("服务器已启动"+"-"*100)

client_socket,client_addr=server_socket.accept()

print("开始传输文件")
with open("tlp.png",'rb') as file:
    bytes=file.read(10)
    
    while bytes:
        client_socket.send(bytes)
        bytes=file.read(10)
    
    
print("传输结束")
client_socket.close()
server_socket.close()
print("服务器结束"+"-"*100)


#客户端
from socket import *

client_socket=socket()
client_socket.connect(("127.0.0.1",8888))
print("与服务器连接成功"+"-"*100)

with open("zzp.png",'wb') as file:
    data=client_socket.recv(10)
    while data:
        file.write(data)
        data=client_socket.recv(10)
print("阅读完成")

client_socket.close()

文章来源:https://blog.csdn.net/weixin_51992178/article/details/135434127
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。