简体   繁体   English

Python 请求在“获取”方法上返回 401 代码

[英]Python Requests Returning 401 code on 'get' method

I'm working on a webscrape function that's going to be pulling HTML data from internal (non public) servers.我正在研究一个网络爬虫 function,它将从内部(非公共)服务器中提取 HTML 数据。 I have a connection through a VPN and proxy server so when I ping any public site I get code 200 no problem, but our internals are returning 401.我通过 VPN 和代理服务器建立了连接,因此当我 ping 任何公共站点时,我得到代码 200 没问题,但我们的内部返回 401。

Heres my code:这是我的代码:

http_str = f'http://username:password@proxy.yourorg.com:80'

proxyDict = {
    'http' : http_str, 
    'https' : https_str, 
    'ftp' : https_str
    }

html_text = requests.get(url, verify=True, proxies=proxyDict, auth=HTTPBasicAuth(user, pwd))

I've tried flushing my DNS server, using different certificate chains (that had a whole new list of problems).我尝试使用不同的证书链(有一个全新的问题列表)刷新我的 DNS 服务器。 I'm using urllib3 on version 1.23 because that seemed to help with SSL errors.我在 1.23 版上使用 urllib3,因为这似乎有助于解决 SSL 错误。 I've considered using a requests session but I'm not sure what that would change.我考虑过使用请求 session 但我不确定这会改变什么。

Also, the url's we're trying to access DO NOT require a log in. I'm not sure why its throwing 401 errors but the auth is for the proxy server, I think.此外,我们尝试访问的 url 不需要登录。我不知道为什么它会抛出 401 错误,但我认为身份验证是针对代理服务器的。 Any help or idea are appreciated, along with questions as at this point I'm not even sure what to ask to move this along.感谢任何帮助或想法,以及在这一点上的问题,我什至不知道要问什么来推动它。

Edit: the proxyDict has a string with the user and pwd passed it for each type, https http fts, etc.编辑:proxyDict 有一个用户字符串,pwd 为每种类型传递它,https http fts 等。

To use HTTP Basic Auth with your proxy, use the http://user:password@host/ syntax in any of the proxy configuration entries.要将 HTTP 基本身份验证与您的代理一起使用,请在任何代理配置条目中使用http://user:password@host/语法。 See apidocs .请参阅apidocs

import requests
proxyDict = {
    "http": "http://username:password@proxy.yourorg.com:80",
    "https": "http://username:password@proxy.yourorg.com:80"
}
url = 'http://myorg.com/example'
response = requests.get(url, proxies=proxyDict)

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

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