httplib實現(xiàn)了HTTP和HTTPS的客戶端協(xié)議,一般不直接使用,在python更高層的封裝模塊中(urllib,urllib2)使用了它的http實現(xiàn)。 import httplib conn = httplib.HTTPConnection("google.com") conn.request('get', '/') print conn.getresponse().read() conn.close() httplib.HTTPConnection ( host [ , port [ , strict [ , timeout ]]] ) HTTPConnection類的構造函數(shù),表示一次與服務器之間的交互,即請求/響應。參數(shù)host表示服務器主機,如:http://www.csdn.net/;port為端口號,默認值為80; 參數(shù)strict的 默認值為false, 表示在無法解析服務器返回的狀態(tài)行時( status line) (比較典型的狀態(tài)行如: HTTP/1.0 200 OK ),是否拋BadStatusLine 異常;可選參數(shù)timeout 表示超時時間。 HTTPConnection.request ( method , url [ , body [ , headers ]] ) 調用request 方法會向服務器發(fā)送一次請求,method 表示請求的方法,常用有方法有get 和post ;url 表示請求的資源的url ;body 表示提交到服務器的數(shù)據(jù),必須是字符串(如果method 是”post” ,則可以把body 理解為html 表單中的數(shù)據(jù));headers 表示請求的http 頭。 import httplib conn = httplib.HTTPConnection("www.g.com", 80, False) conn.request('get', '/', headers = {"Host": "www.google.com", "User-Agent": "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1) Gecko/20090624 Firefox/3.5", "Accept": "text/plain"}) res = conn.getresponse() print'version:', res.version print'reason:', res.reason print'status:', res.status print'msg:', res.msg print'headers:', res.getheaders() #html #print '\n' + '-' * 50 + '\n' #print res.read() conn.close() Httplib模塊中還定義了許多常量,如: req = urllib2.Request('http://') response = urllib2.urlopen(req) the_page = response.read() FTP同樣: req = urllib2.Request('ftp://') urlopen返回的應答對象response有兩個很有用的方法info()和geturl() values ={'body' : 'test short talk','via':'xxxx'} data = urllib.urlencode(values) req = urllib2.Request(url, data) get方式: data['name'] ='Somebody Here' data['location'] ='Northampton' data['language'] ='Python' url_values = urllib.urlencode(data) url ='http:///example.cgi' full_url = url +'?'+ url_values data = urllib2.open(full_url) 使用Basic HTTP Authentication: import urllib2 # Create an OpenerDirector with support for Basic HTTP Authentication... auth_handler = urllib2.HTTPBasicAuthHandler() auth_handler.add_password(realm='PDQ Application', uri='https:///vecrty.py', user='user', passwd='pass') opener = urllib2.build_opener(auth_handler) # ...and install it globally so it can be used with urlopen. urllib2.install_opener(opener) urllib2.urlopen('http://www. /app.html') 使用代理ProxyHandler: proxy_handler = urllib2.ProxyHandler({'http': 'http://www.:3128/'}) proxy_auth_handler = urllib2.HTTPBasicAuthHandler() proxy_auth_handler.add_password('realm', 'host', 'username', 'password') opener = build_opener(proxy_handler, proxy_auth_handler) # This time, rather than install the OpenerDirector, we use it directly: opener.open('http://www./login.html') URLError–HTTPError: from urllib2 import Request, urlopen, URLError, HTTPError req = Request(someurl) try: response = urlopen(req) except HTTPError, e: print'Error code: ', e.code except URLError, e: print'Reason: ', e.reason else: ............. 或者: from urllib2 import Request, urlopen, URLError req = Request(someurl) try: response = urlopen(req) except URLError, e: if hasattr(e, 'reason'): print'Reason: ', e.reason elif hasattr(e, 'code'): print'Error code: ', e.code else: ............. 通常,URLError在沒有網絡連接(沒有路由到特定服務器),或者服務器不存在的情況下產生 req = urllib2.Request('http://') try: urllib2.urlopen(req) except URLError, e: print e.reason print e.code print e.read() 最后需要注意的就是,當處理URLError和HTTPError的時候,應先處理HTTPError,后處理URLError class HTTPHandler(AbstractHTTPHandler): def http_open(self, req): return self.do_open(httplib.HTTPConnection, req) http_request = AbstractHTTPHandler.do_request_ HTTPHandler是Openers當中的默認控制器之一,看到這個代碼,證實了urllib2是借助于httplib實現(xiàn)的,同時也證實了Openers和Handlers的關系。 |
|