米扑代理示例(mimvp-proxy-demo)
米扑代理示例(mimvp-proxy-demo)聚合了多种编程语言使用代理IP,由北京米扑科技有限公司(mimvp.com)原创分享。
米扑代理示例,包含Python、Java、PHP、C#、Go、Perl、Ruby、Shell、NodeJS、PhantomJS、Groovy、Delphi、易语言等十多种编程语言或脚本,通过大量的可运行实例,详细讲解了使用代理IP的正确方法,方便网页爬取、数据采集、自动化测试等领域。
米扑代理示例,测试使用的代理IP,全部来自于米扑代理:http://proxy.mimvp.com
米扑代理示例官网 : http://proxy.mimvp.com/demo2.php
编程语言之代理协议
编程语言之代理示例
1. 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 ); // 返回网页内容
$result = curl_exec ( $ch );
}
2. Python 设置代理
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网址抛出异常
# 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网页,不能是https网页
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)
3. Java 设置代理
@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";
// 设置系统代理,支持全部协议 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 设置登录代理服务器的用户名和密码
// 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 =