如何在requests.session中使用代理?

huangapple go评论78阅读模式
英文:

How do i use proxy with requests.session

问题

嗨,我尝试在会话中执行以下操作:
<code>with requests.Session() as ses:</code>
    <code>s = ses.get(f"URL")</code>

我的代理格式是:http://username:password@url_of_proxyprovider:port

我尝试在这种情况下使用以下代码:<code>ses.proxies.update(proxy)</code>,但没有起作用。
也尝试过这个,但没效果:<code>s.proxies = {'proxy'}</code>
还尝试过像这样添加代理:<code>s = ses.get(f"url", proxies=proxy)</code>
始终使用我的IP,而不是代理。
英文:

Hi i am trying to do an action with a session like this:
<code>with requests.Session() as ses:</code>
<code>s = ses.get(f"URL")</code>

my proxy format is: http://username:password@url_of_proxyprovider:port

i tried using in this case: <code>ses.proxies.update(proxy)</code> but it didnt work.
also tried this, but didnt work: <code>s.proxies = {'proxy'}</code>
also tried adding proxy like this: <code>s = ses.get(f"url", proxies=proxy)</code>
its always done through my IP and not through the proxy.

答案1

得分: 0

将评论转换为答案。

正确的方法是:

import requests

with requests.Session() as session:
    session.proxies = {
        'http': 'http://username:password@url_of_proxyprovider:port',
        'https': 'http://username:password@url_of_proxyprovider:port'
    }

    url = 'http://mywebsite.com/example'
    response = session.get(url)

如果你想代理FTP,也可以将其添加到proxies字典中:

session.proxies = {
    'http': 'http://username:password@url_of_proxyprovider:port',
    'https': 'http://username:password@url_of_proxyprovider:port',
    'ftp': 'http://username:password@url_of_proxyprovider:port'
}
英文:

Converting comment to answer.

The correct way to do it:

import requests

with requests.Session() as session
    session.proxies = {
        &#39;http&#39;: &#39;http://username:password@url_of_proxyprovider:port&#39;,
        &#39;https&#39;: &#39;http://username:password@url_of_proxyprovider:port&#39;
    }

    url = &#39;http://mywebsite.com/example&#39;
    response = session.get(url)

If you want to proxy FTP also, you can add it to the proxies dictionary:

    session.proxies = {
        &#39;http&#39;: &#39;http://username:password@url_of_proxyprovider:port&#39;,
        &#39;https&#39;: &#39;http://username:password@url_of_proxyprovider:port&#39;,
        &#39;ftp&#39;: &#39;http://username:password@url_of_proxyprovider:port&#39;
    }

答案2

得分: 0

以下是翻译好的代码部分:

这里是一些示例

import requests
proxies = {'http': 'http://10.11.4.254:3128'}
s = requests.session()
s.proxies.update(proxies)
s.get("http://www.example.com")

或者在每个会话请求中传递代理

import requests
proxies = {'http': 'http://10.11.4.254:3128'}
s = requests.session()
s.get("http://www.example.com", proxies=proxies)
英文:

Here are some example of it.

    import requests
    proxies = {&#39;http&#39;: &#39;http://10.11.4.254:3128&#39;}
    s = requests.session()
    s.proxies.update(proxies)
    s.get(&quot;http://www.example.com&quot;)

or with each session request you can pass proxy

    import requests
    proxies = {&#39;http&#39;: &#39;http://10.11.4.254:3128&#39;}
    s = requests.session()
    s.get(&quot;http://www.example.com&quot;,proxies=proxies)

huangapple
  • 本文由 发表于 2023年7月6日 19:02:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定