标签:网络编程
贴代码,server端:
import socket # s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) host = ‘‘ port = 6074 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.bind((host,port)) s.listen(1) while 1: conn,addr = s.accept() while 1: print (‘connected by‘,addr) data = conn.recv(1024) if not data:break conn.sendall(data) conn.close()
客户端:
import socket # s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) host = ‘‘ port = 6074 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s.bind((host,port)) s.listen(1) while 1: conn,addr = s.accept() while 1: print (‘connected by‘,addr) data = conn.recv(1024) if not data:break conn.sendall(data) conn.close()
for循环是在一个线程下执行,不同于多线程。
本文出自 “秋风习习” 博客,请务必保留此出处http://aitiankong.blog.51cto.com/8923090/1565268
【学习笔记】Python网络编程(二)socket处理多个连接
标签:网络编程
原文地址:http://aitiankong.blog.51cto.com/8923090/1565268