test_urllib2.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:mechanize 作者: python-mechanize 项目源码 文件源码
def test_redirected_robots_txt(self):
        # redirected robots.txt fetch shouldn't result in another attempted
        # robots.txt fetch to check the redirection is allowed!
        import mechanize
        from mechanize import build_opener, HTTPHandler, \
            HTTPDefaultErrorHandler, HTTPRedirectHandler, \
            HTTPRobotRulesProcessor

        class MockHTTPHandler(mechanize.BaseHandler):
            def __init__(self):
                self.requests = []

            def http_open(self, req):
                import mimetools
                import httplib
                import copy
                from StringIO import StringIO
                self.requests.append(copy.deepcopy(req))
                if req.get_full_url() == "http://example.com/robots.txt":
                    hdr = "Location: http://example.com/en/robots.txt\r\n\r\n"
                    msg = mimetools.Message(StringIO(hdr))
                    return self.parent.error("http", req,
                                             test_response(), 302, "Blah", msg)
                else:
                    return test_response("Allow: *", [], req.get_full_url())

        hh = MockHTTPHandler()
        hdeh = HTTPDefaultErrorHandler()
        hrh = HTTPRedirectHandler()
        rh = HTTPRobotRulesProcessor()
        o = build_test_opener(hh, hdeh, hrh, rh)
        o.open("http://example.com/")
        self.assertEqual([req.get_full_url() for req in hh.requests], [
            "http://example.com/robots.txt",
            "http://example.com/en/robots.txt",
            "http://example.com/",
        ])
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号