ashttpcli.py

来自「python web programming 部分」· Python 代码 · 共 51 行

PY
51
字号
import asyncore
import socket

class http_client (asyncore.dispatcher):

    def __init__ (self, host, path, cnum):
        asyncore.dispatcher.__init__ (self)
        self.path = path
        self.cnum = cnum
        self.host = host
        self.wflag = 1
        self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
        self.connect ((self.host, 80))

    def handle_connect (self):
        self.send ('GET %s HTTP/1.0\r\n\r\n' % self.path)
        self.wflag = 0

    def handle_read (self):
        data = self.recv (8192)
        if not data:
            return ""
        lines = data.split("\n")
        for line in lines:
            if line.find("<H1>") >= 0:
                print "Channel:", self.cnum, ">>>", line

    def handle_close(self):
        self.close()

    def handle_write (self):
        return

    def writable(self):
        return self.wflag

import sys
import urlparse
cnum = 0
for url in ["http://127.0.0.1/lrtest/"]*10:
    parts = urlparse.urlparse (url)
    if parts[0] != 'http':
        raise ValueError, "HTTP URL's only, please"
    else:
        cnum += 1
        host = parts[1]
        path = parts[2]
        http_client (host, path, cnum)
asyncore.loop()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?