如何使用 auth 制作 urllib POST?

how to make urllib POST with auth?

我正在尝试使用 urllib (python 3.5), but can't figure it out. There are plenty examples on how to make either POST call without auth or call with auth, but GET 进行 POST 调用...我正在努力将 2 和 2 放在一起!有人可以帮我提供代码吗?

你可以试试:

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)