python网络编程实例简析,python编程实例简析
python网络编程实例简析,python编程实例简析
本文实例讲述了python网络编程,分享给大家供大家参考。
具体方法如下:
服务端代码如下:
from SocketServer import(TCPServer as TCP, StreamRequestHandler as SRH) from time import ctime HOST = '' PORT = 21567 ADDR = (HOST, PORT) class MyRequestHandle(SRH): def handle(self): print 'connecting from ..', self.client_address self.wfile.write("[%s]:%s" % (ctime(),self.rfile.readline()) ) tcp_Server = TCP(ADDR,MyRequestHandle) print 'WAITING connecting...' tcp_Server.serve_forever()
客户端代码如下:
from socket import * HOST = 'localhost' PORT = 21567 BUFSIZE = 1024 ADDR = (HOST, PORT) while True: tcpCliSock = socket(AF_INET,SOCK_STREAM) tcpCliSock.connect(ADDR) data = raw_input('>>>') if not data: break tcpCliSock.send("%s\r\n" % data) data = tcpCliSock.recv(BUFSIZE) if not data: break print data.strip() tcpCliSock.close()
希望本文所述对大家的Python程序设计有所帮助。
取下在个twisted的电子书。
如果只是简单的socket编程,找些博客文章就行了!
你的例子没问题啊
while 1:
这一段就是一个infinite loop阿
只要client(就是浏览器)和server(就是你的程序)仍然有通信,while循环就不结束
所以你看到的就是卡住的效果,其实是因为server一直等着接收client发的数据
所以只有你强制关掉server程序,这个while循环才退出。
通常,如果你要发送很长的请求到server,是需要某种方法表示出你的数据的长度,一种是Unique End-of-string Identifier (就是字符串结束标记,在数据尾部),一种是 Size Indicator(在数据开始时告诉server你的数据有多长)
建议你仔细看下相关的文档或书拉,这里很难解释清楚。
推荐Python网络编程基础:
www.douban.com/subject/2137490/
英文版可以从我的网盘下:
dl.getdropbox.com/...g.djvu
相关内容
- Python实现的一个简单LRU cache,pythonlrucache
- Python实现从url中提取域名的几种方法,pythonurl
- Python中unittest用法实例,pythonunittest
- Python中itertools模块用法详解,pythonitertools
- python迭代器实例简析,python实例简析
- python实现在无须过多援引的情况下创建字典的方法,
- python对字典进行排序实例,python排序实例
- python实现忽略大小写对字符串列表排序的方法,python大
- python实现给字典添加条目的方法,python字典
- python中bisect模块用法实例,pythonbisect
评论关闭