python requests header

这篇博客详细介绍了使用Python requests库进行HTTP请求时Header的作用。内容涵盖了常见的HTTP响应头,如Content-Type(application/zip)、Content-Disposition(attachment,用于设置文件下载)以及Set-Cookie、Cache-Control等,同时提到了安全相关的Header,如Content-Security-Policy和Strict-Transport-Security,强调了它们在保护网络安全中的角色。
url = "https://www.dropbox.com/sh/5shvr16c0xtv0lr/AAD8CcuyPqDTf0xbiitBZkVta?dl=1"
ua = 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'
header = {"User-Agent": ua}
r = requests.get(url, headers=header)

for key in r.headers:
    print("{} - {}".format(str(key), r.headers[key]))

Server - nginx
Date - Wed, 15 May 2019 01:51:12 GMT
Content-Type - application/zip
Transfer-Encoding - chunked
Connection - keep-alive
x-robots-tag - noindex, nofollow, noimageindex
content-disposition - attachment; filename=iPhone.zip
set-cookie - uc_session=XKqn5nlvHUy7RRSKHhWEXd8Uh4704oGu6UWtuuXgWYPjXzQ92Lymm148KGoPZi7H; Domain=dropboxusercontent.com; httponly; Path=/; secure
content-security-policy - sandbox allow-forms allow-scripts allow-top-navigation allow-popups ; report-uri https://www.dropbox.com/csp_log?policy_name=blockserver-usercontent, form-action 'none' ; report-uri https://www.dropbox.com/csp_log?policy_name=blockserver-noscript ; script-src 'none'
x-dropbox-request-id - 74b33acbba1edf11a9c4b7ed075f1933
pragma - public
cache-control - max-age=0
referrer-policy - no-referrer
Vary - Origin
X-Server-Response-Time - 263
Strict-Transport-Security - max-age=15552000; includeSubDomains

 

import time

headers = dict()
headers["Content-Type"] = "text/html; charset=utf-8"
headers["Content-Type"] = "application/zip"
headers["content-disposition"] = "attachment; filename=iPhone.zip"


file_name = time.strftime("%y%m%d%H%M%S", time.localtime())
if "content-disposition" in headers:
    for item in headers["content-disposition"].split("; "):
        if item.split("=")[0] == "filename":
            file_name = item.split("=")[1]
print("file_name={}".format(file_name))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值