如何使用auth进行urllib POST?

时间:2017-08-14 13:58:32

标签: python python-3.x urllib

我尝试使用urllib(python 3.5)进行POST调用,但无法弄清楚。有很多关于如何制作POST call without authcall with auth, but GET的例子......我很难将2和2放在一起!有人可以帮我一个代码吗?

1 个答案:

答案 0 :(得分:3)

您可以尝试:

import urllib.request  

auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='name',
                          uri='url',
                          user='user',
                          passwd='pass')
opener = urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)
req = urllib.request.Request(url=some_url, data=data, method='POST')
urllib.request.urlopen(req)