當前位置: 首頁>>代碼示例>>Python>>正文


Python httplib.HTTP屬性代碼示例

本文整理匯總了Python中httplib.HTTP屬性的典型用法代碼示例。如果您正苦於以下問題:Python httplib.HTTP屬性的具體用法?Python httplib.HTTP怎麽用?Python httplib.HTTP使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在httplib的用法示例。


在下文中一共展示了httplib.HTTP屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: post_multipart

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def post_multipart(host, selector, fields, files):  
    """ 
    Post fields and files to an http host as multipart/form-data. 
    fields is a sequence of (name, value) elements for regular form fields. 
    files is a sequence of (name, filename, value) elements for data to be uploaded as files 
    Return the server's response page. 
    """
    content_type, body = encode_multipart_formdata(fields, files)  
    h = httplib.HTTP(host)  
    h.putrequest('POST', selector)  
    h.putheader('content-type', content_type)  
    h.putheader('content-length', str(len(body)))  
    h.endheaders()  
    h.send(body)  
    errcode, errmsg, headers = h.getreply()
    return h.file.read() 
開發者ID:cisp,項目名稱:LinuxEmergency,代碼行數:18,代碼來源:postfile.py

示例2: encode_multipart_formdata

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def encode_multipart_formdata(fields, files):  
    """ 
    fields is a sequence of (name, value) elements for regular form fields. 
    files is a sequence of (name, filename, value) elements for data to be uploaded as files 
    Return (content_type, body) ready for httplib.HTTP instance 
    """  
    BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'  
    CRLF = '\r\n'  
    L = []  
    for (key, value) in fields:  
        L.append('--' + BOUNDARY)  
        L.append('Content-Disposition: form-data; name="%s"' % key)  
        L.append('')  
        L.append(value)  
    for (key, filename, value) in files:  
        L.append('--' + BOUNDARY)  
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))  
        L.append('Content-Type: %s' % get_content_type(filename))  
        L.append('')  
        L.append(value)  
    L.append('--' + BOUNDARY + '--')  
    L.append('')  
    body = CRLF.join(L)  
    content_type = 'multipart/form-data; boundary=%s' % BOUNDARY  
    return content_type, body 
開發者ID:cisp,項目名稱:LinuxEmergency,代碼行數:27,代碼來源:postfile.py

示例3: redirect_internal

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def redirect_internal(self, url, fp, errcode, errmsg, headers, data):
        if 'location' in headers:
            newurl = headers['location']
        elif 'uri' in headers:
            newurl = headers['uri']
        else:
            return
        void = fp.read()
        fp.close()
        # In case the server sent a relative URL, join with original:
        newurl = basejoin(self.type + ":" + url, newurl)

        # For security reasons we do not allow redirects to protocols
        # other than HTTP, HTTPS or FTP.
        newurl_lower = newurl.lower()
        if not (newurl_lower.startswith('http://') or
                newurl_lower.startswith('https://') or
                newurl_lower.startswith('ftp://')):
            raise IOError('redirect error', errcode,
                          errmsg + " - Redirection to url '%s' is not allowed" %
                          newurl,
                          headers)

        return self.open(newurl) 
開發者ID:glmcdona,項目名稱:meddle,代碼行數:26,代碼來源:urllib.py

示例4: redirect_internal

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def redirect_internal(self, url, fp, errcode, errmsg, headers, data):
        if 'location' in headers:
            newurl = headers['location']
        elif 'uri' in headers:
            newurl = headers['uri']
        else:
            return
        fp.close()
        # In case the server sent a relative URL, join with original:
        newurl = basejoin(self.type + ":" + url, newurl)

        # For security reasons we do not allow redirects to protocols
        # other than HTTP, HTTPS or FTP.
        newurl_lower = newurl.lower()
        if not (newurl_lower.startswith('http://') or
                newurl_lower.startswith('https://') or
                newurl_lower.startswith('ftp://')):
            raise IOError('redirect error', errcode,
                          errmsg + " - Redirection to url '%s' is not allowed" %
                          newurl,
                          headers)

        return self.open(newurl) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:25,代碼來源:urllib.py

示例5: test_ipv6host_header

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def test_ipv6host_header(self):
        # Default host header on IPv6 transaction should be wrapped by [] if
        # it is an IPv6 address
        expected = 'GET /foo HTTP/1.1\r\nHost: [2001::]:81\r\n' \
                   'Accept-Encoding: identity\r\n\r\n'
        conn = httplib.HTTPConnection('[2001::]:81')
        sock = FakeSocket('')
        conn.sock = sock
        conn.request('GET', '/foo')
        self.assertTrue(sock.data.startswith(expected))

        expected = 'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \
                   'Accept-Encoding: identity\r\n\r\n'
        conn = httplib.HTTPConnection('[2001:102A::]')
        sock = FakeSocket('')
        conn.sock = sock
        conn.request('GET', '/foo')
        self.assertTrue(sock.data.startswith(expected)) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:20,代碼來源:test_httplib.py

示例6: test_parse_all_octets

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def test_parse_all_octets(self):
        # Ensure no valid header field octet breaks the parser
        body = (
            b'HTTP/1.1 200 OK\r\n'
            b"!#$%&'*+-.^_`|~: value\r\n"  # Special token characters
            b'VCHAR: ' + bytearray(range(0x21, 0x7E + 1)) + b'\r\n'
            b'obs-text: ' + bytearray(range(0x80, 0xFF + 1)) + b'\r\n'
            b'obs-fold: text\r\n'
            b' folded with space\r\n'
            b'\tfolded with tab\r\n'
            b'Content-Length: 0\r\n'
            b'\r\n'
        )
        sock = FakeSocket(body)
        resp = httplib.HTTPResponse(sock)
        resp.begin()
        self.assertEqual(resp.getheader('Content-Length'), '0')
        self.assertEqual(resp.getheader("!#$%&'*+-.^_`|~"), 'value')
        vchar = ''.join(map(chr, range(0x21, 0x7E + 1)))
        self.assertEqual(resp.getheader('VCHAR'), vchar)
        self.assertIsNotNone(resp.getheader('obs-text'))
        folded = resp.getheader('obs-fold')
        self.assertTrue(folded.startswith('text'))
        self.assertIn(' folded with space', folded)
        self.assertTrue(folded.endswith('folded with tab')) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:27,代碼來源:test_httplib.py

示例7: test_status_lines

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def test_status_lines(self):
        # Test HTTP status lines

        body = "HTTP/1.1 200 Ok\r\n\r\nText"
        sock = FakeSocket(body)
        resp = httplib.HTTPResponse(sock)
        resp.begin()
        self.assertEqual(resp.read(0), '')  # Issue #20007
        self.assertFalse(resp.isclosed())
        self.assertEqual(resp.read(), 'Text')
        self.assertTrue(resp.isclosed())

        body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
        sock = FakeSocket(body)
        resp = httplib.HTTPResponse(sock)
        self.assertRaises(httplib.BadStatusLine, resp.begin) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:18,代碼來源:test_httplib.py

示例8: test_host_port

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def test_host_port(self):
        # Check invalid host_port

        # Note that httplib does not accept user:password@ in the host-port.
        for hp in ("www.python.org:abc", "user:password@www.python.org"):
            self.assertRaises(httplib.InvalidURL, httplib.HTTP, hp)

        for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", "fe80::207:e9ff:fe9b",
                          8000),
                         ("www.python.org:80", "www.python.org", 80),
                         ("www.python.org", "www.python.org", 80),
                         ("www.python.org:", "www.python.org", 80),
                         ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80)):
            http = httplib.HTTP(hp)
            c = http._conn
            if h != c.host:
                self.fail("Host incorrectly parsed: %s != %s" % (h, c.host))
            if p != c.port:
                self.fail("Port incorrectly parsed: %s != %s" % (p, c.host)) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:21,代碼來源:test_httplib.py

示例9: test_response_headers

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def test_response_headers(self):
        # test response with multiple message headers with the same field name.
        text = ('HTTP/1.1 200 OK\r\n'
                'Set-Cookie: Customer="WILE_E_COYOTE";'
                ' Version="1"; Path="/acme"\r\n'
                'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";'
                ' Path="/acme"\r\n'
                '\r\n'
                'No body\r\n')
        hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"'
               ', '
               'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"')
        s = FakeSocket(text)
        r = httplib.HTTPResponse(s)
        r.begin()
        cookies = r.getheader("Set-Cookie")
        if cookies != hdr:
            self.fail("multiple headers not combined properly") 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:20,代碼來源:test_httplib.py

示例10: test_chunked_head

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def test_chunked_head(self):
        chunked_start = (
            'HTTP/1.1 200 OK\r\n'
            'Transfer-Encoding: chunked\r\n\r\n'
            'a\r\n'
            'hello world\r\n'
            '1\r\n'
            'd\r\n'
        )
        sock = FakeSocket(chunked_start + '0\r\n')
        resp = httplib.HTTPResponse(sock, method="HEAD")
        resp.begin()
        self.assertEqual(resp.read(), '')
        self.assertEqual(resp.status, 200)
        self.assertEqual(resp.reason, 'OK')
        self.assertTrue(resp.isclosed()) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:18,代碼來源:test_httplib.py

示例11: test_local_bad_hostname

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def test_local_bad_hostname(self):
        # The (valid) cert doesn't validate the HTTP hostname
        import ssl
        server = self.make_server(CERT_fakehostname)
        context = ssl.SSLContext(ssl.PROTOCOL_TLS)
        context.verify_mode = ssl.CERT_REQUIRED
        context.check_hostname = True
        context.load_verify_locations(CERT_fakehostname)
        h = httplib.HTTPSConnection('localhost', server.port, context=context)
        with self.assertRaises(ssl.CertificateError):
            h.request('GET', '/')
        h.close()
        # With context.check_hostname=False, the mismatching is ignored
        context.check_hostname = False
        h = httplib.HTTPSConnection('localhost', server.port, context=context)
        h.request('GET', '/nonexistent')
        resp = h.getresponse()
        self.assertEqual(resp.status, 404) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:20,代碼來源:test_httplib.py

示例12: do_search

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def do_search(self):
        h = httplib.HTTP(self.server)
        h.putrequest('GET', "/search/web/results/?q=%40" + self.word +
                     "&elements_per_page=50&start_index=" + str(self.counter))
        h.putheader('Host', self.hostname)
        h.putheader(
            'Referer',
            "http://" +
            self.hostname +
            "/search/web/results/?q=%40" +
            self.word)
        h.putheader('User-agent', self.userAgent)
        h.endheaders()
        returncode, returnmsg, headers = h.getreply()
        self.results = h.getfile().read()
        self.totalresults += self.results 
開發者ID:Yukinoshita47,項目名稱:Yuki-Chan-The-Auto-Pentest,代碼行數:18,代碼來源:exaleadsearch.py

示例13: do_search_files

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def do_search_files(self, files):
        h = httplib.HTTP(self.server)
        h.putrequest(
            'GET',
            "search/web/results/?q=" +
            self.word +
            "filetype:" +
            self.files +
            "&elements_per_page=50&start_index=" +
            self.counter)
        h.putheader('Host', self.hostname)
        h.putheader('User-agent', self.userAgent)
        h.endheaders()
        returncode, returnmsg, headers = h.getreply()
        self.results = h.getfile().read()
        self.totalresults += self.results 
開發者ID:Yukinoshita47,項目名稱:Yuki-Chan-The-Auto-Pentest,代碼行數:18,代碼來源:exaleadsearch.py

示例14: test_ipv6host_header

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def test_ipv6host_header(self):
        # Default host header on IPv6 transaction should wrapped by [] if
        # its actual IPv6 address
        expected = 'GET /foo HTTP/1.1\r\nHost: [2001::]:81\r\n' \
                   'Accept-Encoding: identity\r\n\r\n'
        conn = httplib.HTTPConnection('[2001::]:81')
        sock = FakeSocket('')
        conn.sock = sock
        conn.request('GET', '/foo')
        self.assertTrue(sock.data.startswith(expected))

        expected = 'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \
                   'Accept-Encoding: identity\r\n\r\n'
        conn = httplib.HTTPConnection('[2001:102A::]')
        sock = FakeSocket('')
        conn.sock = sock
        conn.request('GET', '/foo')
        self.assertTrue(sock.data.startswith(expected)) 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:20,代碼來源:test_httplib.py

示例15: __str__

# 需要導入模塊: import httplib [as 別名]
# 或者: from httplib import HTTP [as 別名]
def __str__(self):
        return 'HTTP Error %s: %s' % (self.code, self.msg) 
開發者ID:war-and-code,項目名稱:jawfish,代碼行數:4,代碼來源:urllib2.py


注:本文中的httplib.HTTP屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。