服务端的源码:
import socketserver class Handler_TCPServer(socketserver.BaseRequestHandler): """ The TCP Server class for demonstration. Note: We need to implement the Handle method to exchange data with TCP client. """ def handle(self): # self.request - TCP socket connected to the client self.data = self.request.recv(1024).strip() print("{} sent:".format(self.client_address[0])) print(self.data) # just send back ACK for data arrival confirmation self.request.sendall("ACK from TCP Server".encode()) if __name__ == "__main__": HOST, PORT = "localhost", 9999 # Init the TCP server object, bind it to the localhost on 9999 port tcp_server = socketserver.TCPServer((HOST, PORT), Handler_TCPServer) # Activate the TCP server. # To abort the TCP server, press Ctrl-C. tcp_server.serve_forever()
客户端的源码:
import socket host_ip, server_port = "127.0.0.1", 9999 data = " Hello how are you?\n" # Initialize a TCP client socket using SOCK_STREAM tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: # Establish connection to TCP server and exchange data tcp_client.connect((host_ip, server_port)) tcp_client.sendall(data.encode()) # Read data from the TCP server and close the connection received = tcp_client.recv(1024) finally: tcp_client.close() print ("Bytes Sent: {}".format(data)) print ("Bytes Received: {}".format(received.decode()))
作者:虚生 出处:https://www.cnblogs.com/dylancao/ 以音频和传感器算法为核心的智能可穿戴产品解决方案提供商 ,提供可穿戴智能软硬件解决方案的设计,开发和咨询服务。 勾搭热线:邮箱:1173496664@qq.com weixin:18019245820 市场技术对接群:347609188 |
![]() |
分类:
python
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2020-02-01 django+celery+ RabbitMQ实现异步任务实例