简体   繁体   English

如何在Mac OSX中使用Python 2.7更改代理设置

[英]How to change proxy settings using Python 2.7 in Mac OSX

I am using selenium webdriver in python scripts. 我在python脚本中使用selenium webdriver。 I want to change proxy settings to vpn from python before visiting any website so that when visit any website through webdriver they will detect according to vpn ip address. 我想在访问任何网站之前将代理设置从python更改为vpn,这样当通过webdriver访问任何网站时,他们将根据vpn ip地址进行检测。

Can anybody please help me to do this. 任何人都可以帮我这样做。 Thanks in advance for your help. 在此先感谢您的帮助。

Mac代理设置

As you are using python binding, you need to use the remote object to set the proxy. 在使用python绑定时,需要使用远程对象来设置代理。

the other way is to change the python binding code itself. 另一种方法是更改​​python绑定代码本身。

Following code will change the proxy 以下代码将更改代理

from selenium import webdriver

PROXY = "localhost:8080"

# Create a copy of desired capabilities object.
desired_capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER.copy()
# Change the proxy properties of that copy.
desired_capabilities['proxy'] = {
    "httpProxy":PROXY,
    "ftpProxy":PROXY,
    "sslProxy":PROXY,
    "noProxy":None,
    "proxyType":"MANUAL",
    "class":"org.openqa.selenium.Proxy",
    "autodetect":False
}

# you have to use remote, otherwise you'll have to code it yourself in python to 
# dynamically changing the system proxy preferences
driver = webdriver.Remote("http://localhost:4444/wd/hub", desired_capabilities)

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

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