Python httplib模組使用執行個體

來源:互聯網
上載者:User
httplib模組是一個底層基礎模組,實現的功能比較少,正常情況下比較少用到.推薦用urllib, urllib2, httplib2.

HTTPConnection 對象

class httplib.HTTPConnection(host[, port[, strict[, timeout[, source_address]]]])

建立HTTPConnection對象

HTTPConnection.request(method, url[, body[, headers]])

發送請求

HTTPConnection.getresponse()

獲得響應

HTTPResponse對象

HTTPResponse.read([amt])
Reads and returns the response body, or up to the next amt bytes.

HTTPResponse.getheader(name[, default])

獲得指定頭資訊

HTTPResponse.getheaders()

獲得(header, value)元組的列表

HTTPResponse.fileno()

獲得底層socket檔案描述符

HTTPResponse.msg

獲得頭內容

HTTPResponse.version

獲得頭http版本

HTTPResponse.status

獲得返回狀態代碼

HTTPResponse.reason

獲得返回說明

執行個體

代碼如下:


#!/usr/bin/python
import httplib

conn = httplib.HTTPConnection("www.bitsCN.com")
conn.request("GET", "/")
r1 = conn.getresponse()

print r1.status, r1.reason
print '-' * 40

headers = r1.getheaders()
for h in headers:
print h
print '-' * 40

print r1.msg

輸出:

代碼如下:


200 OK
----------------------------------------
('content-length', '106883')
('accept-ranges', 'bytes')
('vary', 'Accept-Encoding, Accept-Encoding')
('keep-alive', 'timeout=20')
('server', 'ngx_openresty')
('last-modified', 'Fri, 10 Apr 2015 09:30:10 GMT')
('connection', 'keep-alive')
('etag', '"55279822-1a183"')
('date', 'Fri, 10 Apr 2015 09:48:15 GMT')
('content-type', 'text/html; charset=utf-8')
----------------------------------------
Server: ngx_openresty
Date: Fri, 10 Apr 2015 09:48:15 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 106883
Connection: keep-alive
Keep-Alive: timeout=20
Vary: Accept-Encoding
Last-Modified: Fri, 10 Apr 2015 09:30:10 GMT
Vary: Accept-Encoding
ETag: "55279822-1a183"
Accept-Ranges: bytes

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.