码迷,mamicode.com
首页 > 编程语言 > 详细

Python并发编程-IO模型-IO多路复用实现SocketServer

时间:2018-09-28 16:28:59      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:read   end   hello   sock   复用   reading   sockets   img   bin   

技术分享图片

  • Server.py
import select
import socket

sk = socket.socket()
sk.bind(('127.0.0.1',8080))
sk.setblocking(False)
sk.listen()
read_lst = [sk] #select监听谁就放入list
while True: #[sk,conn]
    r_lst,w_lst,x_lsx = select.select(read_lst,[],[])
    # print('*******',r_lst)
    # print(sk)
    for i in r_lst:
        if i is sk:
            conn,addr = i.accept()
            # print(conn)
            read_lst.append(conn)
        else:
            ret = i.recv(1024)
            if ret == b'':
                i.close()
                read_lst.remove(i)
                continue
            print(ret)
            i.send(b'goodbye')
  • client.py
import socket
import time
import threading
def func():
    sk = socket.socket()
    sk.connect(('127.0.0.1',8080))
    sk.send(b'hello')
    time.sleep(1)
    print(sk.recv(1024))
    sk.close()

for i in range(20):
    threading.Thread(target=func).start()

Python并发编程-IO模型-IO多路复用实现SocketServer

标签:read   end   hello   sock   复用   reading   sockets   img   bin   

原文地址:https://www.cnblogs.com/konglinqingfeng/p/9718775.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!