How to use the mechanize.polyglot.create_response_info function in mechanize

To help you get started, we’ve selected a few mechanize examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github python-mechanize / mechanize / test / test_urllib2.py View on Github external
def http_message(mapping):
    """
    >>> http_message({"Content-Type": "text/html"}).items()
    [('content-type', 'text/html')]

    """
    f = []
    for kv in iteritems(mapping):
        f.append("%s: %s" % kv)
    f.append("")
    msg = "\r\n".join(f)
    if not isinstance(msg, bytes):
        msg = msg.encode('iso-8859-1')
    msg = create_response_info(BytesIO(msg))
    return msg
github python-mechanize / mechanize / test / test_urllib2.py View on Github external
def http_open(self, req):
        import copy
        self.requests.append(copy.deepcopy(req))
        if self._count == 0:
            self._count = self._count + 1
            name = "Not important"
            msg = create_response_info(BytesIO(
                self.headers.encode('iso-8859-1')))
            return self.parent.error("http", req,
                                     test_response(), self.code, name, msg)
        else:
            self.req = req
            return test_response("", [], req.get_full_url())
github python-mechanize / mechanize / test / test_browser.py View on Github external
def test_select_form(self):
        from mechanize import _response
        br = TestBrowser()
        fp = BytesIO(b'''
            <form name="a"></form>
            <form data-ac="123" name="b"></form>
            <form class="x" name="c"></form>
            ''')
        headers = create_response_info(
            BytesIO(b"Content-type: text/html"))
        response = _response.response_seek_wrapper(
            _response.closeable_response(fp, headers, "http://example.com/",
                                         200, "OK"))
        br.set_response(response)
        for i, n in enumerate('abc'):
            br.select_form(nr=i)
            self.assertEqual(br.form.name, n)
            br.select_form(nr=0), br.select_form(name=n)
            self.assertEqual(br.form.name, n)
        br.select_form(data_ac=re.compile(r'\d+'))
        self.assertEqual(br.form.name, 'b')
        br.select_form(class_=lambda x: x == 'x')
        self.assertEqual(br.form.name, 'c')
github python-mechanize / mechanize / test / test_browser.py View on Github external
def getresponse(self_):
            class Response(object):
                msg = create_response_info(BytesIO(b""))
                status = 200
                reason = "OK"
                fp = BytesIO(b'')

                def read(self__, sz=-1):
                    return b""

                def close(self):
                    self.fp = None

                def readinto(self__, b):
                    pass

            return Response()
github python-mechanize / mechanize / test / test_urllib2.py View on Github external
def http_open(self, req):
                import copy
                self.requests.append(copy.deepcopy(req))
                if req.get_full_url() == "http://example.com/robots.txt":
                    hdr = b"Location: http://example.com/en/robots.txt\r\n\r\n"
                    msg = create_response_info(BytesIO(hdr))
                    return self.parent.error("http", req,
                                             test_response(), 302, "Blah", msg)
                else:
                    return test_response("Allow: *", [], req.get_full_url())
github python-mechanize / mechanize / mechanize / _urllib2_fork.py View on Github external
fw = self.connect_ftp(user, passwd, host, port, dirs, req.timeout)
            type = file and 'I' or 'D'
            for attr in attrs:
                attr, value = splitvalue(attr)
                if attr.lower() == 'type' and \
                   value in ('a', 'A', 'i', 'I', 'd', 'D'):
                    type = value.upper()
            fp, retrlen = fw.retrfile(file, type)
            headers = ""
            mtype = mimetypes.guess_type(req.get_full_url())[0]
            if mtype:
                headers += "Content-type: %s\n" % mtype
            if retrlen is not None and retrlen >= 0:
                headers += "Content-length: %d\n" % retrlen
            sf = BytesIO(headers.encode('iso-8859-1'))
            headers = create_response_info(sf)
            return closeable_response(fp, headers, req.get_full_url())
        except ftplib.all_errors as msg:
            raise_with_traceback(URLError('ftp error: %s' % msg))
github python-mechanize / mechanize / mechanize / _urllib2_fork.py View on Github external
def open_local_file(self, req):
        import email.utils as emailutils
        import mimetypes
        host = req.get_host()
        file = req.get_selector()
        try:
            localfile = url2pathname(file)
        except IOError as err:
            # url2pathname raises this on windows for bad urls
            raise URLError(err)
        try:
            stats = os.stat(localfile)
            size = stats.st_size
            modified = emailutils.formatdate(stats.st_mtime, usegmt=True)
            mtype = mimetypes.guess_type(file)[0]
            headers = create_response_info(BytesIO(
                ('Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
                    (mtype or 'text/plain', size, modified)).encode(
                        'iso-8859-1')))
            if host:
                host, port = splitport(host)
            if not host or (
                    not port and socket.gethostbyname(host) in self.get_names()
            ):
                fp = open(localfile, 'rb')
                return closeable_response(fp, headers, 'file:' + file)
        except OSError as msg:
            # urllib2 users shouldn't expect OSErrors coming from urlopen()
            raise URLError(msg)
        raise URLError('file not on local host')
github python-mechanize / mechanize / mechanize / _response.py View on Github external
def make_headers(headers):
    """
    headers: sequence of (name, value) pairs
    """
    hdr_text = []
    for name_value in headers:
        hdr_text.append("%s: %s" % name_value)
    ans = "\n".join(hdr_text)
    if not isinstance(ans, bytes):
        ans = ans.encode('iso-8859-1')
    return create_response_info(BytesIO(ans))