本文实例讲述了python使用socket向客户端发送数据的方法。分享给大家供大家参考。具体如下:
import socket, sysport = 55555host = 'localhost'data = "test" * 10485760 # 40MB of datas = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect((host, port))byteswritten = 0while 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程序设计有所帮助。
新闻热点
疑难解答
图片精选