python???selectors

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python???selectors相关的知识,希望对你有一定的参考价值。

?????????connect   leo   odi   tor   ??????   nts   sock   ??????   net   

selectors???select?????????????????????ptython?????????????????????????????????selectors?????????????????????selectors

??????????????????

# -*- coding: utf-8 -*-
__author__ = ???fc???

import selectors
import socket
import time

def accept(sock:socket.socket, mask):
    conn, addr = sock.accept()
    print(???client???, addr, ???connected???)
    conn.setblocking(False)
    sel.register(conn, selectors.EVENT_READ, oper)

def oper(sock:socket.socket, mask):
    if selectors.EVENT_READ & mask:
        print(???ready to read???)
        data = sock.recv(1024)
        if not data:
            print(???client???, sock.getpeername(), ???disconnnected???)
            sel.unregister(sock)
            sock.close()
        else:
            print(???received: ???, data)
            sel.modify(sock, selectors.EVENT_WRITE, oper)
    elif selectors.EVENT_WRITE & mask:
        print(???ready to write???)
        msg = b???hello, friend???
        sock.send(msg)
        print(???send: ???, msg)
        sel.modify(sock, selectors.EVENT_READ, oper)


sel = selectors.DefaultSelector()
print(sel)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
sock.bind((??????, 60001))
sock.listen(5)
sock.setblocking(False)
sel.register(sock, selectors.EVENT_READ, accept)

while True:
    events = sel.select(10)
    if not events:
        print(???timeout???, time.strftime(???%Y-%m-%d %H:%M:%S???))
    for key, mask in events:
        callback = key.data
        callback(key.fileobj, mask)

 

以上是关于python???selectors的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

python之selectors模块

python异步编程--回调模型(selectors模块)

(转)python异步编程--回调模型(selectors模块)

Selenium,webdriver - 找不到 - xpath / css_selector / ... python

常用python日期日志获取内容循环的代码片段