📄 ashttpcli.py
字号:
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)
print "Channel:", self.cnum, \
"Sent request for", self.path, "to", self.host
self.wflag = 0
def handle_read (self):
data = self.recv (8192)
print "Channel:", self.cnum, "Received", len(data), "bytes"
def handle_write (self):
print "Channel:", self.cnum, "was writable"
def writable(self):
return self.wflag
import sys
import urlparse
cnum = 0
for url in sys.argv[1:]:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -