python使用socket向客户端发送数据的方法
作者:重负在身 时间:2023-08-13 00:16:51
本文实例讲述了python使用socket向客户端发送数据的方法。分享给大家供大家参考。具体如下:
import socket, sys
port = 55555
host = 'localhost'
data = "test" * 10485760 # 40MB of data
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
byteswritten = 0
while byteswritten < len(data):
startpos = byteswritten
endpos = min(byteswritten + 1024, len(data))
byteswritten += s.send(data[startpos:endpos])
sys.stdout.write("Wrote %d bytes\r" % byteswritten)
sys.stdout.flush()
s.shutdown(1)
print "All data sent."
while 1:
buf = s.recv(1024)
if not len(buf):
break
sys.stdout.write(buf)
希望本文所述对大家的Python程序设计有所帮助。
标签:python,socket
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL远程无法连接的一些常见原因总结
2024-01-24 21:24:21
![](https://img.aspxhome.com/file/2023/4/79144_0s.png)
Python requests.post方法中data与json参数区别详解
2022-02-06 09:06:20
Django利用AJAX技术实现博文实时搜索
2023-01-02 01:57:50
![](https://img.aspxhome.com/file/2023/3/80583_0s.jpg)
基于Python的科学占卜工具开发过程
2023-01-01 03:15:05
![](https://img.aspxhome.com/file/2023/2/72092_0s.gif)
如何用Matlab和Python读取Netcdf文件
2023-03-19 11:19:30
![](https://img.aspxhome.com/file/2023/8/90448_0s.png)
python dataframe astype 字段类型转换方法
2022-02-19 07:58:50
在Django中自定义filter并在template中的使用详解
2021-01-15 19:17:45
![](https://img.aspxhome.com/file/2023/7/114647_0s.jpg)
matlab xlabel位置的设置方式
2022-06-11 06:51:24
![](https://img.aspxhome.com/file/2023/1/112921_0s.png)
Python ArcPy实现批量计算多时相遥感影像的各项元平均值
2022-06-04 14:45:51
![](https://img.aspxhome.com/file/2023/5/80755_0s.png)
python可视化大屏库big_screen示例详解
2021-10-16 14:32:39
![](https://img.aspxhome.com/file/2023/7/87487_0s.jpg)
python排序函数sort()与sorted()的区别
2023-09-08 23:38:08
Python爬虫:通过关键字爬取百度图片
2022-06-16 11:32:47
![](https://img.aspxhome.com/file/2023/9/70019_0s.png)
Python环境下安装PyGame和PyOpenGL的方法
2021-12-09 10:14:38
![](https://img.aspxhome.com/file/2023/3/81693_0s.png)
MySQL 查询速度慢的原因
2024-01-25 01:10:27
php中$_GET与$_POST过滤sql注入的方法
2023-07-13 14:38:12
错误22022 SQLServerAgent当前未运行的解决方法
2024-01-27 21:19:32
MySql常用命令总结
2009-09-16 10:51:00
简单了解python 邮件模块的使用方法
2021-07-11 00:08:14
python运行或调用另一个py文件或参数方式
2023-10-26 02:04:47
![](https://img.aspxhome.com/file/2023/0/82780_0s.png)
Windows10下安装配置 perl 环境的详细教程
2022-11-17 01:46:32
![](https://img.aspxhome.com/file/2023/0/65830_0s.jpg)