2017-08-14 146 views
0

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

+0

https://stackoverflow.com/questions/3238925/python-urllib-urllib2-post –

+1

'auth'在'requests'要容易得多。你可以使用 - http://docs.python-requests.org/en/master/user/authentication/。你也可以发布你的代码,所以我们有什么想法你试过吗? – droravr

+0

感谢您的回复,但我必须使用urllib,而不是请求。 – uzla

回答

1

你可以试试:

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)