TCP服务端例子与TCP客户端发送信息例子

TCP服务端例子

(ps:仅有代码,小伙伴们利用百度,冲啊!!!)

#coding=utf-8
from socket import *

server_socket = socket(AF_INET,SOCK_STREAM)  # 建立TCP套接字
server_socket.bind(("127.0.0.1",8890))  # #本机监听8899端口
server_socket.listen(5)
print("等待接收连接!")
client_socket,client_info = server_socket.accept()
recv_data = client_socket.recv(1024)  #最大接收1024字节
print(f"收到信息:{recv_data.decode('gbk')},来自:{client_info}")

client_socket.close()
server_socket.close()

TCP客户端发送信息例子

(ps:仅有代码,小伙伴们利用百度,冲啊!!!)


from socket import *

client_socket = socket(AF_INET,SOCK_STREAM)
client_socket.connect(("127.0.0.1",8899))

client_socket.send("hello".encode("gbk"))
client_socket.close()

你可能感兴趣的:(网络编程萌新级,tcp/ip,网络,python)