37,721
社区成员
发帖
与我相关
我的任务
分享
# -*- coding:cp936 -*-
import asyncore, socket
class http_client(asyncore.dispatcher): #定义了一个http_client类
def __init__(self, host, path):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, 80))
self.buffer = 'GET % HTTP/1.0 \r\n\r\n' % path
def handle_connect(self): #链接调用接口
pass
def handle_close(): #接口关闭方法
self.close()
def handle_read(self):
print self.recv(8192)
def writable(self):
return (len(self.buffer)>0)
def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
if __name__ == '__main__':
c = http_client(' snaps.php.net', '/')
asyncore.loop()
print 'program exit'
---------- Python ----------
Traceback (most recent call last):
File "1.2.2.py", line 21, in <module>
c = http_client(' snaps.php.net', '/')
File "1.2.2.py", line 8, in __init__
self.buffer = 'GET % HTTP/1.0 \r\n\r\n' % path
ValueError: unsupported format character 'H' (0x48) at index 6
输出完成 (耗时 0 秒) - 正常终止