国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python基于select實現的socket服務器

2019-11-25 16:50:44
字體:
來源:轉載
供稿:網友

本文實例講述了Python基于select實現的socket服務器。分享給大家供大家參考,具體如下:

借鑒了asyncore模塊中select.select的使用方法

import socketimport tracebackimport selectEOL1 = b'/n/n'EOL2 = b'/n/r/n'socketmap = {}r,w,e = [],[],[]response = b'HTTP/1.0 200 OK/r/nDate: Mon, 1 Jan 1996 01:01:01 GMT/r/n'response += b'Content-Type: text/plain/r/nContent-Length: 13/r/n/r/n'response += b'Hello, world!'serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)serversocket.bind(('0.0.0.0', 23456))serversocket.listen(1)#serversocket.setblocking(0)listening_fileno = serversocket.fileno()socketmap[listening_fileno] = serversocketprint 'listening_fileno',listening_filenotry:  while True:    r,w,e = [],[],[]    for fd in socketmap:      r.append(fd)      w.append(fd)      e.append(fd)    r,w,e = select.select(r,w,e,1)    for fd in r:      request = b''      isocket = socketmap[fd]      if fd == listening_fileno:        print 'accepting'        clientsock,clientaddr = isocket.accept()        #clientsock.setblocking(0)        cli_fileno = clientsock.fileno()        r.append(cli_fileno)        w.append(cli_fileno)        e.append(cli_fileno)        socketmap[cli_fileno] = clientsock      else:        print 'reading'        while EOL1 not in request and EOL2 not in request:          request += isocket.recv(1024)        print(request.decode())    for fd in w:      print 'writing'      osocket = socketmap[fd]      osocket.send(response)    for fd in e:      esocket = socketmap[fd]      print 'socket close',fd      esocket.close()      del socketmap[fd]    print "no data coming"except Exception,e:  print traceback.print_exc()  serversocket.close()

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吴忠市| 始兴县| 兴隆县| 壤塘县| 特克斯县| 陕西省| 乐安县| 上蔡县| 蒙阴县| 清涧县| 昌都县| 昌邑市| 江川县| 平罗县| 林周县| 托克逊县| 德格县| 饶阳县| 峨边| 克什克腾旗| 乌鲁木齐市| 迭部县| 防城港市| 连江县| 大庆市| 花莲市| 长治市| 九龙城区| 安顺市| 繁峙县| 宜兴市| 察哈| 南城县| 南部县| 东丽区| 巨鹿县| 黑水县| 武义县| 黄浦区| 封丘县| 化州市|