如何设置代理IP?常用工具及语言

日期: 2020-09-15 14:18

本文列举常用语言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](http://doc.scrapy.org/en/latest/topics/downloader-middleware.html#scrapy.contrib.downloadermiddleware.httpproxy.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')}"

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

%>

相关新闻