Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Python Socket编程实现的简单tcp迭代服务器

Python Socket编程实现的简单tcp迭代服务器与C/C++ Socket编程对比见 http://www.linuxidc.com/Linux/2014-10/107871.htm服务器: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() 客户端: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核心编程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm《Python开发技术详解》.( 周伟,宗杰).[高清PDF扫描版+随书视频+代码] http://www.linuxidc.com/Linux/2013-11/92693.htmPython脚本获取Linux系统信息 http://www.linuxidc.com/Linux/2013-08/88531.htm在Ubuntu下用Python搭建桌面算法交易研究环境 http://www.linuxidc.com/Linux/2013-11/92534.htmPython 语言的发展简史 http://www.linuxidc.com/Linux/2014-09/107206.htmPython 的详细介绍:请点这里
Python 的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-10/107870.htm