简体   繁体   English

PYTHON:请求和响应 401

[英]PYTHON: requests and response 401

I have a little problem with authentication.我的身份验证有点问题。 I am writting a script, which is getting login and password from user(input from keyboard) and then I want to get some data from the website(http not https), but every time I run the script the response is 401.I read some similar posts from stack and I tried this solutions:我正在编写一个脚本,它从用户那里获取登录名和密码(从键盘输入),然后我想从网站上获取一些数据(http 不是 https),但是每次运行脚本时,响应都是 401。我读了堆栈中的一些类似帖子,我尝试了以下解决方案:

Solution 1解决方案1

c = HTTPConnection("somewebsite")
userAndPass = b64encode(b"username:password").decode("ascii")
headers = { 'Authorization' : 'Basic %s' %  userAndPass }
c.request('GET', '/', headers=headers)
res = c.getresponse()
data = res.read()

Solution 2解决方案2

with requests.Session() as c:
    url = 'somewebsite'
    USERNAME = 'username'
    PASSWORD = 'password'
    c.get(url)
    login_data = dict(username = USERNAME, password = PASSWORD)
    c.post(url,data = login_data)
    page = c.get('somewebsite', headers = {"Referer": "somwebsite"})
    print(page)

Solution 3解决方案3

www = 'somewebsite'
value ={'filter':'somefilter'}
data = urllib.parse.urlencode(value)
data=data.encode('utf-8')
req = urllib.request.Request(www,data)
resp = urllib.request.urlopen(req)
respData = resp.read()
print(respData)
x = urllib.request.urlopen(www,"username","password")
print(x.read())'

I don't know how to solve this problem.我不知道如何解决这个问题。 Can somebody give me some link or tip ?有人可以给我一些链接或提示吗?

Have you tried the Basic Authentication example from requests?您是否尝试过请求中的基本身份验证示例?

>>> from requests.auth import HTTPBasicAuth
>>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass'))
<Response [200]>

Can I know what type of authentication on the website?我可以知道网站上的身份验证类型吗?

this is an official Basic Auth example ( http://docs.python-requests.org/en/master/user/advanced/#http-verbs )这是一个官方的基本身份验证示例( http://docs.python-requests.org/en/master/user/advanced/#http-verbs

from requests.auth import HTTPBasicAuth
auth = HTTPBasicAuth('fake@example.com', 'not_a_real_password')

r = requests.post(url=url, data=body, auth=auth)
print(r.status_code)

To use api with authentication, we need to have token_id or app_id that will provide the access for our request.要将 api 与身份验证一起使用,我们需要有 token_id 或 app_id 来为我们的请求提供访问权限。 Below is an example how we can formulate the url and get the response: strong text下面是我们如何制定 url 并获得响应的示例:强文本

import requests
city = input()
api_call = "http://api.openweathermap.org/data/2.5/weather?"
app_id = "892d5406f4811786e2b80a823c78f466"
req_url = api_call + "q=" + city + "&appid=" + app_id
response = requests.get(req_url)
data = response.json()
if (data["cod"] == 200):
    hum = data["main"]["humidity"]
    print("Humidity is % d " %(hum))
elif data["cod"] != 200:
    print("Error occurred : " ,data["cod"], data["message"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM