博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 中的socket
阅读量:4616 次
发布时间:2019-06-09

本文共 1087 字,大约阅读时间需要 3 分钟。

python中利用socket模块来实现对各种底层通讯的封装,支持tcp/udp协议,为编制c/s类的程序提供了便利。

最常见的用法:

如ftp_server:

1 import socketserver 2 class Mysocketserver(socketserver.BaseRequestHandler): 3     def handle(self): 4         while True: 5             try: 6                 self.data=self.request.recv(1024).strip() 7                 print(self.client_address[0],'recv>>',self.data) 8                 self.request.send(self.data.upper()) 9             except Exception as e:10                 print("客户端关闭了!",e)11                 break12 ipaddr='localhost'13 port=999914 # server=socketserver.TCPServer((ipaddr,port),Mysocketserver)15 server=socketserver.ThreadingTCPServer((ipaddr,port),Mysocketserver)16 server.serve_forever()17 server.close_request()
ftp_server_code

ftp_client

1 import socket 2  3 client = socket.socket() 4 client.connect(('localhost', 9999)) 5 while True: 6     data = input(">>>>>>") 7     if len(data) == 0: continue 8     client.send(data.encode("utf-8")) 9     re_data = client.recv(1024)10     print(re_data)11 client.close()
ftp_client_code

 

转载于:https://www.cnblogs.com/lzszs/p/8972917.html

你可能感兴趣的文章
Notepad++使用NppFTP插件编辑linux上的文件
查看>>
NPOI 操作Excel
查看>>
MySql【Error笔记】
查看>>
vue入门
查看>>
JS线程Web worker
查看>>
Flex的动画效果与变换!(三)(完)
查看>>
mysql常见错误码
查看>>
Openresty 与 Tengine
查看>>
使用XV-11激光雷达做hector_slam
查看>>
布局技巧4:使用ViewStub
查看>>
ddt Ui 案例2
查看>>
拿下主机后内网的信息收集
查看>>
LeetCode 876. Middle of the Linked List
查看>>
作业一
查看>>
joj1023
查看>>
动画原理——旋转
查看>>
Finding LCM LightOJ - 1215 (水题)
查看>>
python生成器
查看>>
PowerDesigner Constraint name uniqueness 错误
查看>>
系统子系统_GPRS子系统流程图
查看>>