如何设置代理IP?常用工具及语言
2019-01-24 本文已影响0人
佑岷
本文列举常用语言or工具如何配置or设置代理IP。
- cURL
curl -x http://PROXYHOST:PORT -U USERNAME:PASSWORD http://example.com
- Wget
http_proxy="http://PROXYHOST:PORT" wget --proxy-user=USERNAME --proxy-password=PASSWORD http://example.com
- Python
>>> import requests
>>> proxies = {'http': 'http://USERNAME:PASSWORD@PROXYHOST:PORT', 'https': 'http://USERNAME:PASSWORD@PROXYHOST:PORT'}
>>> response = requests.get('http://example.com', proxies=proxies)
- Scrapy
参见:HttpProxyMiddleware - Ruby
方法一:
Using HTTParty
HTTParty.get(url, { http_proxyaddr: "PROXYHOST", http_proxyport: "PORT", http_proxyuser: "USERNAME", http_proxypass: "PASSWORD", :timeout => 3 })
方法二:
Using typhoeus
options = {proxy: 'http://PROXYHOST:PORT', proxyuserpwd: 'USERNAME:PASSWORD'}
req = Typhoeus::Request.new(url, options)
req.run
- PhantomJS and CasperJS
PhantomJS:
> page.customHeaders={'Proxy-Authorization': 'Basic '+btoa('USERNAME:PASSWORD')};
CasperJS:
casper.page.customHeaders = 'Proxy-Authorization': "Basic #{btoa('USERNAME:PASSWORD')}"
- PHP 略
- Java
HttpClient client = new HttpClient();
HttpConnectionManager conManager = client.getHttpConnectionManager();
client.getHostConfiguration().setProxy("PROXYHOST", PORT);
HttpState state = new HttpState();
state.setProxyCredentials(null, null, new UsernamePasswordCredentials("USERNAME", "PASSWORD"));
client.setState(state);
- C#
WebProxy ProxyString = new WebProxy("http://PROXYHOST:PORT", true);
//set network credentials may be optional
NetworkCredential proxyCredential = new NetworkCredential("USERNAME", "PASSWORD");
ProxyString.Credentials = proxyCredential;
WebRequest.DefaultWebProxy = ProxyString;
HttpWebRequest request = (HttpWebRequest);
//manually set authorization header
string authInfo = "USERNAME" + ":" + "PASSWORD";
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
request.Headers["Proxy-Authorization"] = "Basic " + authInfo;
- ASP VBScript
<%
Const HTTPREQUEST_PROXYSETTING_PROXY = 2
Const HTTPREQUEST_SETCREDENTIALS_FOR_PROXY = 1
Dim responseText
With Server.CreateObject("WinHttp.WinHttpRequest.5.1")
.SetProxy HTTPREQUEST_PROXYSETTING_PROXY, "PROXYHOST:PORT"
.Open "GET", "http://example.com"
.SetCredentials "USERNAME", "PASSWORD", HTTPREQUEST_SETCREDENTIALS_FOR_PROXY
.Send
responseText = .ResponseText
End With
%>