2017-08-14 143 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)