How do I set headers using python's urllib?

前端 未结 4 1809
逝去的感伤
逝去的感伤 2020-11-28 22:06

I am pretty new to python\'s urllib. What I need to do is set a custom header for the request being sent to the server. Specifically, I need to set the Content-type and Au

4条回答
  •  孤独总比滥情好
    2020-11-28 22:46

    For multiple headers do as follow:

    import urllib2
    req = urllib2.Request('http://www.example.com/')
    req.add_header('param1', '212212')
    req.add_header('param2', '12345678')
    req.add_header('other_param1', 'sample')
    req.add_header('other_param2', 'sample1111')
    req.add_header('and_any_other_parame', 'testttt')
    resp = urllib2.urlopen(req)
    content = resp.read()
    

提交回复
热议问题