欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python 僅獲取響應(yīng)頭, 不獲取實(shí)體的實(shí)例

 更新時(shí)間:2019年08月21日 15:53:28   作者:zhipeng-python  
今天小編就為大家分享一篇Python 僅獲取響應(yīng)頭, 不獲取實(shí)體的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

Python Just get Response Headers, not get content.

1. Use HEAD method

>>> import requests
>>> res = requests.head("http://www.baidu.com/")
>>> req.head("https://www.baidu.com/").headers
{'Content-Encoding': 'gzip', 'Server': 'bfe/1.0.8.18', 'Last-Modified': 'Mon, 13 Jun 2016 02:50:08 GMT', 'Connection': 'Keep-Alive', 'Pragma': 'no-cache', 'Cache-Control': 'private, no-cache, no-store, proxy-revalidate, no-transform', 'Date': 'Fri, 13 Oct 2017 04:36:20 GMT', 'Content-Type': 'text/html'}
>>> res.ok
True
>>> res.content
''
# 但是會(huì)遇到一些問題, 比如, 服務(wù)器不支持 HEAD, 或者拒絕 HEAD.
# 如下情況就被拒絕
#
>>> res = req.head("https://www.douban.com/subject/1/")
>>> res
<Response [403]>
>>> res.ok
False
>>> res.content
''
>>> res.headers
{'Content-Encoding': 'gzip', 'Keep-Alive': 'timeout=30', 'Server': 'dae', 'Connection': 'keep-alive', 'Date': 'Fri, 13 Oct 2017 04:39:00 GMT', 'Content-Type': 'text/html'}

不是很通用, 因?yàn)橛行┓?wù)器不支持.

2. Use urllib

import urllib
>>> res = urllib.urlopen("http://127.0.0.1:8000/git.exe")
>>> res.url
'http://127.0.0.1:8000/git.exe'
>>> res.headers.headers
['Server: SimpleHTTP/0.6 Python/2.7.10\r\n', 'Date: Fri, 13 Oct 2017 06:06:37 GMT\r\n', 'Content-type: application/x-msdownload\r\n', 'Content-Length: 7569408\r\n', 'Last-Modified: Fri, 16 Dec 2016 07:09:32 GMT\r\n']
>>> len(r.read())
7569408
# urllib 只有在調(diào)用 read/readline/readlines 的時(shí)候才會(huì)從 web 服務(wù)器讀取數(shù)據(jù).
# 源碼可以在 urllib/httplib 中找到. 
# urllib.py
def urlopen(url, ...):
 opener = FancyURLopener()
 return opener.open(url)
class FancyURLopener(URLopener).open():
 getattr(self, name)(url)
class URLopener.open_http():
 errcode, errmsg, headers = h.getreply()
 if(200 <= errcode < 300):
  return addinfourl(fp, headers, "http:" + url, errcode)
 else:
  if data is None:
   return self.http_error(url, fp, errcode, errmsg, headers)
  else:
   return self.http_error(url, fp, errcode, errmsg, headers, data)
class URLopener.http_error():
 return method(url, fp, errcode, errmsg, headers)
class FancyURLopener.http_error_default():
 return addinfourl(fp, headers, "http:" + url, errcode)
class addinfourl(addbase):
 # 代碼中并沒有對(duì) fp 做任何操作,包括讀寫. 
class addbase.__init __():
 self.fp = fp
 self.read = self.fp.read
 self.readline = self.fp.readline
 if hasattr(self.fp, "readlines"): self.readlines = self.fp.readlines
  self.fileno = self.fp.fileno
 # ... ...

可以看到, urllib.open 最終返回了 addbase, addbase 中沒有對(duì) socket 做任務(wù)處理, 不會(huì)有任何讀寫. 之后顯示調(diào)用 read/readline/readlines, 才會(huì)從 web 服務(wù)器讀取數(shù)據(jù).

圖 1. 初始化網(wǎng)絡(luò).

圖 2. urlopen() 之后

圖 3. read() 之后

3. Use socket

看過 urllib 之后, 可以使用 socket 寫一個(gè)方法, 只獲取 header.

import socket
import ssl


_timeout = 10
socket.setdefaulttimeout(_timeout)

def get_header(host, port=80, uri="/", method="GET", user_ssl=False):
 # 這里可以再擴(kuò)充一下, 支持 headers
 conn = None
 header = """%s %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36\r\n\r\n""" % (
  method, uri, host)
 if user_ssl:
  ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
  _socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  conn = ssl_context.wrap_socket(_socket, server_hostname=host)
  conn.connect((host, port))
  conn.send(header)
 else:
  conn = socket.create_connection((host, port), _timeout)
  conn.sendall(header)
 text = ""
 while True:
  if "\r\n\r\n" in text:
   break
  buff = conn.recv(10)
  text += buff
  # print buff
 conn.close()
 return text.split("\r\n\r\n")[0]

if __name__ == '__main__':
 print get_header("www.douban.com", uri="/subject/27076001/")
 print
 print get_header("www.douban.com", uri="/subject/27076001/", port=443, user_ssl=True)
➜ 76[14:48:20]zhipeng@zhipeng-MacBook ~/demo/python
�� $ python test_header.py
HTTP/1.1 301 Moved Permanently
Date: Fri, 13 Oct 2017 06:48:23 GMT
Content-Type: text/html
Content-Length: 178
Connection: close
Location: https://www.douban.com/subject/27076001/
Server: dae

HTTP/1.1 302 Moved Temporarily
Server: ADSSERVER/45863
Date: Fri, 13 Oct 2017 06:48:23 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: close
Location: https://sec.douban.com/b?r=https%3A%2F%2Fwww.douban.com%2Fsubject%2F27076001%2F
Strict-Transport-Security: max-age=15552000;
Set-Cookie: __ads_session=uY8l3pLW/AjCKJ8Y4wA=; domain=.douban.com; path=/
X-Powered-By-ADS: uni-jnads-1-02
➜ 77[14:48:23]zhipeng@zhipeng-MacBook ~/demo/python 
�� $ 

參考

<< Python socket server handle HTTPS request >> (https://stackoverflow.com/questions/32062925/python-socket-server-handle-https-request)

以上這篇Python 僅獲取響應(yīng)頭, 不獲取實(shí)體的實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Pycharm中配置遠(yuǎn)程Docker運(yùn)行環(huán)境的教程圖解

    Pycharm中配置遠(yuǎn)程Docker運(yùn)行環(huán)境的教程圖解

    這篇文章主要介紹了Pycharm中配置遠(yuǎn)程Docker運(yùn)行環(huán)境,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • 用Python實(shí)現(xiàn)校園通知更新提醒功能

    用Python實(shí)現(xiàn)校園通知更新提醒功能

    今天小編就為大家分享一篇用Python實(shí)現(xiàn)校園通知更新提醒功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python實(shí)現(xiàn)打乒乓小游戲

    Python實(shí)現(xiàn)打乒乓小游戲

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)打乒乓小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • python中l(wèi)ower函數(shù)實(shí)現(xiàn)方法及用法講解

    python中l(wèi)ower函數(shù)實(shí)現(xiàn)方法及用法講解

    在本篇文章里小編給大家整理的是一篇關(guān)于python中l(wèi)ower函數(shù)實(shí)現(xiàn)方法及用法講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。
    2020-12-12
  • python中可以聲明變量類型嗎

    python中可以聲明變量類型嗎

    在本篇文章里小編給大家整理了關(guān)于python中聲明變量類型的相關(guān)知識(shí)點(diǎn),需要的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • opencv-python基本圖像處理詳解

    opencv-python基本圖像處理詳解

    這篇文章主要介紹了Python Opencv圖像處理基本操作代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-08-08
  • python函數(shù)裝飾器之帶參數(shù)的函數(shù)和帶參數(shù)的裝飾器用法示例

    python函數(shù)裝飾器之帶參數(shù)的函數(shù)和帶參數(shù)的裝飾器用法示例

    這篇文章主要介紹了python函數(shù)裝飾器之帶參數(shù)的函數(shù)和帶參數(shù)的裝飾器用法,結(jié)合實(shí)例形式分析了Python函數(shù)裝飾器中函數(shù)帶多個(gè)參數(shù)以及裝飾器帶有多個(gè)參數(shù)的具體原理與實(shí)現(xiàn)方法,需要的朋友可以參考下
    2019-11-11
  • python Gunicorn服務(wù)器使用方法詳解

    python Gunicorn服務(wù)器使用方法詳解

    這篇文章主要介紹了python Gunicorn服務(wù)器使用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 如何將tensorflow訓(xùn)練好的模型移植到Android (MNIST手寫數(shù)字識(shí)別)

    如何將tensorflow訓(xùn)練好的模型移植到Android (MNIST手寫數(shù)字識(shí)別)

    這篇文章主要介紹了將tensorflow訓(xùn)練好的模型移植到Android (MNIST手寫數(shù)字識(shí)別),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • 詳解Python如何優(yōu)雅的重試

    詳解Python如何優(yōu)雅的重試

    這篇文章主要為大家介紹了Python如何優(yōu)雅的重試詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07

最新評(píng)論