首页 > 代码库 > python socket编程实现的简单tcp迭代server
python socket编程实现的简单tcp迭代server
与c/c++ socket编程对照见http://blog.csdn.net/aspnet_lyc/article/details/38946915
server:
import socket PORT = 9999 BACKLOG = 5 MAXLINE = 1024 listenfd = socket.socket(socket.AF_INET,socket.SOCK_STREAM) listenfd.bind(('',PORT)) listenfd.listen(BACKLOG) while True: connfd, connaddr = listenfd.accept() print 'a new connection' buf = [] buf = connfd.recv(MAXLINE) print buf connfd.send('Hello,this is server') connfd.close()
client:
import socket addr = '127.0.0.1' port = 9999 sockfd = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sockfd.connect((addr, port)) sockfd.send('Hello,this is client') buf = [] while True: recv_data = sockfd.recv(1024) if recv_data: buf.append(recv_data) else: break data = ''.join(buf) print data sockfd.close()
python socket编程实现的简单tcp迭代server
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。