python select

##socket非阻塞
import socket
from urllib.parse import urlparse

def get_url(url):
    url=urlparse(url)
    host=url.netloc
    path=url.path
    if not path:
        path="/"
    print(host, path)
    client=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    client.setblocking(False)
    try:
        client.connect((host, 80))
    except BlockingIOError as e:
        pass
    while True:
        try:
            client.send("'GET {} HTTP/1.1\r\nHost: {}:{}\r\nConnection: close\r\n\r\n'".format(path,host,80).encode('utf8'))
            break
        except OSError as e:
            continue
    data=b""
    while True:
        try:
            d=client.recv(1024)
        except BlockingIOError as e:
            continue
        if d:
            data+=d
        else:
            break
    print(data.decode('utf8'))
    client.close()
get_url("http://www.baidu.com")


##select
import
socket from urllib.parse import urlparse from selectors import DefaultSelector,EVENT_WRITE,EVENT_READ selector=DefaultSelector() urls=["http://www.baidu.com"] stop=False class Fetcher: def get_url(self,url): self.parseUrl=url url = urlparse(url) self.data=b"" self.host = url.netloc self.path = url.path if not self.path: self.path = "/" self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.client.setblocking(False) try: self.client.connect((self.host, 80)) except BlockingIOError as e: pass selector.register(self.client.fileno(),EVENT_WRITE,self.connected) def readAble(self,key): d = self.client.recv(1024) if d: self.data += d else: selector.unregister(self.client.fileno()) print(self.data.decode('utf8')) self.client.close() urls.remove(self.parseUrl) if not urls: global stop stop=True def connected(self,key): selector.unregister(key.fd) self.client.send( "'GET {} HTTP/1.1\r\nHost: {}:{}\r\nConnection: close\r\n\r\n'".format(self.path, self.host, 80).encode('utf8')) selector.register(self.client.fileno(),EVENT_READ,self.readAble) def loop(): while not stop: status=selector.select() for key,master in status: call_back=key.data call_back(key) if __name__=="__main__": #fetcher=Fetcher() for url in range(20): url="https://www.baidu.com/s?wd=%s"%url urls.append(url) fetcher = Fetcher() fetcher.get_url(url) loop()

 

posted @ 2019-12-02 17:21  howhy  阅读(65)  评论(0)    收藏  举报