米撲代理示例(mimvp-proxy-demo)
米撲代理示例(mimvp-proxy-demo)聚合了多種編程語言使用代理IP非春,由北京米撲科技有限公司(mimvp.com)原創(chuàng)分享柏靶。
米撲代理示例谱醇,包含Python蝶防、Java硕并、PHP纳猪、C#陋葡、Go棋傍、Perl救拉、Ruby、Shell瘫拣、NodeJS亿絮、PhantomJS、Groovy、Delphi壹无、易語言等十多種編程語言或腳本葱绒,通過大量的可運行實例,詳細講解了使用代理IP的正確方法斗锭,方便網(wǎng)頁爬取地淀、數(shù)據(jù)采集、自動化測試等領(lǐng)域岖是。
米撲代理示例帮毁,測試使用的代理IP,全部來自于米撲代理:http://proxy.mimvp.com
米撲代理示例官網(wǎng) :?http://proxy.mimvp.com/demo2.php
$proxy_http = "http://138.68.165.154:3128";
$proxy_https = "https://202.53.169.199:3128";
$proxy_socks4 = "socks4://94.158.70.129:1080";
$proxy_socks5 = "socks5://173.230.95.147:45454";
$mimvp_url = "http://proxy.mimvp.com/exist.php";
$mimvp_url2 = "https://proxy.mimvp.com/exist.php";
// curl
proxy_curl($proxy_http, $mimvp_url); // http
proxy_curl($proxy_https, $mimvp_url); // https
proxy_curl($proxy_socks4, $mimvp_url); // socks4
proxy_curl($proxy_socks5, $mimvp_url); // socks5
// php curl 支持 http豺撑、https烈疚、socks4、socks5
function proxy_curl($proxy_uri, $mimvp_url) {
$key = explode('://', $proxy_uri)[0]; // http
$proxy= explode('://', $proxy_uri)[1]; // ip:port
echo "proxy_uri :? $proxy_uri; key : $key, proxy : $proxy ";
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $mimvp_url);
curl_setopt ( $ch, CURLOPT_HTTPPROXYTUNNEL, false );
curl_setopt ( $ch, CURLOPT_PROXY, $proxy );
if ($key == "http") {
curl_setopt ( $ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP ); // http
}
elseif ($key == "https") {
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS ); // https
}
elseif ($key == "socks4") {
curl_setopt ( $ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4 ); // socks4
}
elseif ($key == "socks5") {
curl_setopt ( $ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5 ); // socks5
}
else {
curl_setopt ( $ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP );
}
curl_setopt ( $ch, CURLOPT_TIMEOUT, 60 );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 60 );
curl_setopt ( $ch, CURLOPT_HEADER, false );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true ); // 返回網(wǎng)頁內(nèi)容
$result = curl_exec ( $ch );
}
proxy_http = {"http":"http://138.68.165.154:3128"}
proxy_https = {"https":"http://191.252.103.93:8080"}
proxy_socks4 = {'socks4': '218.58.52.158:1088'}
proxy_socks5 = {'socks5': '68.234.190.150:45454'}
mimvp_url = "http://proxy.mimvp.com/exist.php"
mimvp_url2 = "https://proxy.mimvp.com/exist.php"
# 全局取消ssl證書驗證聪轿,防止打開未驗證的https網(wǎng)址拋出異常
# urllib2.URLError: [urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)]
ssl._create_default_https_context = ssl._create_unverified_context
# urllib2 支持 http, https
def test_http(proxy, mimvp_url):
handler = urllib2.ProxyHandler(proxy)
opener = urllib2.build_opener(handler)
f = opener.open(mimvp_url, timeout=30)
content = f.read()
print content
print len(content)
f.close()
opener.close()
# urllib 支持 http, https
def test_http2(proxy, mimvp_url):
opener = urllib.FancyURLopener(proxy)
f = opener.open(mimvp_url)? ? ? ? ? ? ? ? #### mimvp_url 只能是http網(wǎng)頁爷肝,不能是https網(wǎng)頁
content = f.read()
print content
print len(content)
f.close()
opener.close()
# socks4
def test_socks4(socks4, mimvp_url):
socks4_ip = socks4.split(":")[0]
socks4_port = int(socks4.split(":")[1])
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, socks4_ip, socks4_port)
socket.socket = socks.socksocket
content = urllib2.urlopen(mimvp_url, timeout=30).read()
print content
print len(content)
# socks5
def test_socks5(socks5, mimvp_url):
socks5_ip = socks5.split(":")[0]
socks5_port = int(socks5.split(":")[1])
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, socks5_ip, socks5_port)
socket.socket = socks.socksocket
content = urllib2.urlopen(mimvp_url, timeout=30).read()
print content
print len(content)
if __name__ == "__main__":
# http, https
test_http(proxy_http, mimvp_url)
test_http(proxy_https, mimvp_url2)
# http
test_http2(proxy_http, mimvp_url)
# socks4
test_socks4(proxy_socks4['socks4'], mimvp_url)
# socks5
test_socks5(proxy_socks5['socks5'], mimvp_url)
@SuppressWarnings({ "serial" })
public static HashMap proxyMap = new HashMap() {
{
put("http", "138.68.161.14:3128");
put("https", "104.236.120.183:8080");
put("socks4", "113.7.118.112:2346");
put("socks5", "61.135.155.82:1080");
}
};
final static String proxyUrl = "http://proxy.mimvp.com/exist.php";
final static String proxyUrl2 = "https://proxy.mimvp.com/exist.php";
// 設(shè)置系統(tǒng)代理,支持全部協(xié)議 http陆错,https灯抛,socks4,socks5
private static int proxy_property(String proxyType, String proxyStr) {
int dataLen = 0;
String proxy_ip = proxyStr.split(":")[0];
String proxy_port = proxyStr.split(":")[1];
Properties prop = System.getProperties();
// http
if(proxyType.equals("http")){
prop.setProperty("http.proxySet", "true");
prop.setProperty("http.proxyHost", proxy_ip);
prop.setProperty("http.proxyPort", proxy_port);
prop.setProperty("http.nonProxyHosts", "localhost|192.168.0.*");
}
// https
if (proxyType.equals("https")) {
prop.setProperty("https.proxyHost", proxy_ip);
prop.setProperty("https.proxyPort", proxy_port);
}
// socks
if(proxyType.equals("socks4") || proxyType.equals("socks5")){
prop.setProperty("socksProxySet", "true");
prop.setProperty("socksProxyHost", proxy_ip);
prop.setProperty("socksProxyPort", proxy_port);
}
// ftp
if(proxyType.equals("ftp")){
prop.setProperty("ftp.proxyHost", proxy_ip);
prop.setProperty("ftp.proxyPort", proxy_port);
prop.setProperty("ftp.nonProxyHosts", "localhost|192.168.0.*");
}
//? ? ? ? // auth 設(shè)置登錄代理服務(wù)器的用戶名和密碼
//? ? ? ? Authenticator.setDefault(new MyAuthenticator("user", "pwd"));
try{
URL url = new URL(proxyUrl2); // http://proxy.mimvp.com
URLConnection conn = url.openConnection();
conn.setConnectTimeout(30 * 1000);
InputStream in = conn.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
char[] ch = new char[1024];
int len = 0;
String data = "";
while((len = reader.read(ch)) > 0) {
String newData = new String(ch, 0, len);
data += newData;
}
System.out.println("data : " + data);
dataLen = data.length();
} catch(Exception e) {
e.printStackTrace();
}
return dataLen;
}
static class MyAuthenticator extends Authenticator {
private String user = "";
private String password = "";
public MyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
}
#!/bin/bash
#
# curl 支持 http音瓷、https对嚼、socks4、socks5
# wget 支持 http绳慎、https
#
# 米撲代理示例:
# http://proxy.mimvp.com/demo2.php
#
# 米撲代理購買:
# http://proxy.mimvp.com
#
# mimvp.com
# 2015-11-09
# http代理格式 http_proxy=http://IP:Port
# https代理格式 https_proxy=http://IP:Port
{'http': 'http://120.77.176.179:8888'}
curl -m 30 --retry 3 -x http://120.77.176.179:8888 http://proxy.mimvp.com/exist.php? ? ? ? # http_proxy
wget -T 30 --tries 3 -e "http_proxy=http://120.77.176.179:8888" http://proxy.mimvp.com/exist.php? # http_proxy
{'https': 'http://46.105.214.133:3128'}
curl -m 30 --retry 3 --proxy-insecure -x http://46.105.214.133:3128 -k https://proxy.mimvp.com/exist.php? ? ? ? # https_proxy
wget -T 30 --tries 3 --no-check-certificate -e "https_proxy=http://46.105.214.133:3128" https://proxy.mimvp.com/exist.php # https_proxy
# curl? 支持socks
{'socks4': '101.255.17.145:1080'}
curl -m 30 --retry 3 --socks4 101.255.17.145:1080 http://proxy.mimvp.com/exist.php
{'socks5': '82.164.233.227:45454'}
curl -m 30 --retry 3 --socks5 82.164.233.227:45454 http://proxy.mimvp.com/exist.php
# wget 不支持socks
################### wget配置文件設(shè)置代理 ###################
vim ~/.wgetrc
http_proxy=http://120.77.176.179:8888:8080
https_proxy=http://12.7.17.17:8888:8080
use_proxy = on
wait = 30
wget -T 30 --tries 3 http://proxy.mimvp.com
################### 設(shè)置臨時局部代理 ###################
# proxy no auth
export http_proxy=http://120.77.176.179:8888:8080
export https_proxy=http://12.7.17.17:8888:8080
# proxy auth
export http_proxy=http://username:password@120.77.176.179:8888:8080
export https_proxy=http://username:password@12.7.17.17:8888:8080
# 取消設(shè)置
unset http_proxy
unset https_proxy
################### 設(shè)置系統(tǒng)全局代理 ###################
# 修改 /etc/profile纵竖,保存并重啟服務(wù)器
sudo vim /etc/profile # 所有人有效
或
sudo vim ~/.bashrc # 所有人有效
或
vim ~/.bash_profile # 個人有效
# proxy no auth
export http_proxy=http://120.77.176.179:8888:8080
export https_proxy=http://12.7.17.17:8888:8080
# proxy auth
export http_proxy=http://username:password@120.77.176.179:8888:8080
export https_proxy=http://username:password@12.7.17.17:8888:8080
source /etc/profile
或
source ~/.bashrc
或
source ~/.bash_profile
sudo reboot
' 易語言 支持 http
'
' 米撲代理示例:
' http://proxy.mimvp.com/demo2.php
'
' 米撲代理購買:
' http://proxy.mimvp.com
'
' mimvp.com
' 2017-07-28
'
' 易語言的安裝與開發(fā),請參考米撲博客:
' http://blog.mimvp.com/2017/08/yi-yu-yan-de-an-zhuang-yu-kai-fa/
'
' 易語言的網(wǎng)絡(luò)編程和代理示例杏愤,請參考米撲博客靡砌,附有源代碼:
' http://blog.mimvp.com/2017/08/yi-yu-yan-de-wang-luo-bian-cheng-he-dai-li-shi-li/
' 變量聲明(表格)
.局部變量 代理地址, 文本型
.局部變量 目標網(wǎng)址, 文本型
.局部變量 代理用戶名, 文本型
.局部變量 代理密碼, 文本型
' 獲取目標網(wǎng)址
.如果 (目標網(wǎng)址編輯框.內(nèi)容 = “”)
目標網(wǎng)址 = “http://proxy.mimvp.com/exist.php”
.否則
目標網(wǎng)址 = 目標網(wǎng)址編輯框.內(nèi)容
' 獲取代理地址
.如果 (代理服務(wù)器編輯框.內(nèi)容 = “”)
代理地址 = “139.59.99.113:8080”
.否則
代理地址 = 代理服務(wù)器編輯框.內(nèi)容
.如果結(jié)束
輸出調(diào)試文本 (“代理地址: ” + 代理地址)
' 設(shè)置代理方法1
網(wǎng)頁_置瀏覽器代理 (代理地址)? ' 通過瀏覽器設(shè)置,一般只支持http代理
超文本瀏覽框.跳轉(zhuǎn) (目標網(wǎng)址編輯框.內(nèi)容, , )
' 設(shè)置代理方法2
輸出調(diào)試文本 (網(wǎng)頁_訪問S (目標網(wǎng)址, , , , , , , , , , 代理地址, , ))
' 設(shè)置代理方式3(用戶名 + 密碼)
輸出調(diào)試文本 (網(wǎng)頁_訪問S (目標網(wǎng)址, , , , , , , , , , 代理地址, 代理用戶名, 代理密碼))
解答:米撲代理示例的引用包或依賴庫乏奥,已經(jīng)放在了mimvp-proxy-demo開源項目里的目錄下了,例如:Python2.7 使用socks4/5是引用了socks.py亥曹,已經(jīng)放在了目錄下:https://github.com/mimvp/mimvp-proxy-demo/tree/master/Python2/socks.py
2. 如何安裝配置編程語言的開發(fā)環(huán)境邓了?
解答:米撲代理使用示例,全部由米撲科技編寫代碼媳瞪、測試驗證骗炉,因此米撲科技搭建了全部的開發(fā)測試環(huán)境,詳細配置編程語言的開發(fā)環(huán)境蛇受,請參考米撲博客:http://blog.mimvp.com
PhantomJS Selenium 設(shè)置動態(tài)代理
Node.js 設(shè)置代理的兩種方式:superagent-proxy 和 https-proxy-agent
Go語言支持 http乍丈、 https剂碴、socks4、socks4a轻专、socks5忆矛,以及嵌套代理
解答:米撲代理使用示例(mimvp-proxy-demo)中的測試代理IP请垛,全部來自米撲代理http://proxy.mimvp.com催训,其支持http、https宗收、socks4漫拭、socks5等全部協(xié)議的代理IP,而且米撲代理覆蓋全球120多個國家混稽,中國34個省市采驻,代理非常豐富,為全球代理IP領(lǐng)導品牌荚坞,推薦挑宠!