简体   繁体   English

如何添加 Pycurl 标头

[英]How to add Pycurl headers

I can't understand this works How to add multiple headers to pycurl HTTPS request, I tried to do it but it accepts the user agent only and gives me an error if I tried to add more headers我不明白这是有效的 如何向 pycurl HTTPS 请求添加多个标头,我尝试这样做,但它只接受用户代理,如果我尝试添加更多标头,它会给我一个错误

url = "https://blabla.com"
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(pycurl.CAINFO, certifi.where())
c.setopt(c.URL, url)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
c.setopt(pycurl.HTTPHEADER, custom_headers)
c.setopt(c.WRITEDATA, buffer)
c.setopt(c.FOLLOWLOCATION, True)
c.perform()
c.close()
body = buffer.getvalue()
response = body.decode("utf-8")
print (response)

Do you have to set the headers as a list您是否必须将标题设置为列表

custom_headers = ['example1: ex1', 'example2: ex2']
curl.setopt(pycurl.HTTPHEADER, custom_headers)

From the pycurl documentation at http://pycurl.io/docs/latest/curlobject.html?highlight=httpheader来自 http 的 pycurl 文档://pycurl.io/docs/latest/curlobject.html?highlight=httpheader

HTTP200ALIASES , HTTPHEADER , POSTQUOTE , PREQUOTE , PROXYHEADER and QUOTE accept a list or tuple of strings. HTTP200ALIASESHTTPHEADERPOSTQUOTEPREQUOTEPROXYHEADERQUOTE接受字符串列表或元组。 The same rules apply to these strings as do to string option values.适用于这些字符串的规则与适用于字符串选项值的规则相同。 Example:例子:

c.setopt(pycurl.HTTPHEADER, ["Accept:"])
c.setopt(pycurl.HTTPHEADER, ("Accept:",))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM