简体   繁体   English

处理是否调用request.get()

[英]Processes if call requests.get()

while working with python Requests, I met the problem with ConnectionRefusedError [WinError 10061] , because of network settings and limitations in my network, or company's network software won't allow it (I think). 在处理python请求时,由于网络设置和网络中的限制,我遇到了ConnectionRefusedError [WinError 10061]问题,或者公司的网络软件不允许这样做(我认为)。

But I was interested in what happens when I call requests.get() . 但是我对调用requests.get()时发生的事情很感兴趣。 Maybe I'm not good at reading the documentation, but I could not find any processes which happen after the call. 也许我不太擅长阅读文档,但找不到呼叫后发生的任何过程。 For example, why if I access URL by the browser it is ok, but while I try to access by requests - it fails. 例如,为什么我通过浏览器访问URL是可以的,但是当我尝试通过请求访问URL时却失败。

What I'm asking about is what processes happen after the call get() method: starts the server at localhost? 我要问的是,在调用get()方法之后,将发生什么过程:在本地主机上启动服务器? configure it? 配置吗? form headers? 表格标题? how it send the request? 它如何发送请求?

Generally, most companies use proxy server for each outgoing request. 通常,大多数公司对每个传出请求都使用代理服务器。 Once set in connection settings, the browsers will read them and set with each request. 一旦设置了连接设置,浏览器将读取它们并为每个请求进行设置。 You can check if proxy is enabled by checking the settings in your browser. 您可以通过检查浏览器中的设置来检查是否启用了代理。

However, when you're making a python request, you will need to set the proxy in the request, like this: 但是,在发出python请求时,需要在请求中设置代理,如下所示:

    proxyDict = { 
                  "http"  : "192.168.100.3:8080", 
                  "https" : "Some/Same proxy for https", 
                  "ftp"   : "Some proxy for ftp (Optional)"
                }

    r = requests.get(url, headers=headers, proxies=proxyDict)

Also, browsers set the content-types, request headers and other such parameters. 同样,浏览器设置内容类型,请求标头和其他此类参数。 You can check browser's developer console, like one of Google Chrome, and goto Network tab and see what all params are being set with the request, and imply the same paramters in your request.get(). 您可以检查浏览器的开发者控制台(例如Google Chrome浏览器之一), 然后转到“网络”标签,查看请求中设置的所有参数,并在request.get()中隐含相同的参数。 In case of headers, it should be : 如果是标题,则应为:

r = requests.get(url, headers=headers, proxies=proxyDict, headers = {'Content-type':'application/json')

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

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