build_opener()和
install_opener()你应该用嘲笑的行为
urlopen()
import urllib2from StringIO import StringIOdef mock_response(req): if req.get_full_url() == "http://example.com": resp = urllib2.addinfourl(StringIO("mock file"), "mock message", req.get_full_url()) resp.pre = 200 resp.msg = "OK" return respclass MyHTTPHandler(urllib2.HTTPHandler): def http_open(self, req): print "mock opener" return mock_response(req)my_opener = urllib2.build_opener(MyHTTPHandler)urllib2.install_opener(my_opener)response=urllib2.urlopen("http://example.com")print response.read()print response.preprint response.msg
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)