Code Usage Examples

Practical code examples for integrating proxies in multiple programming languages.

Code Usage
  • HTTP Proxy: Used for proxying regular web content.
  • HTTPS Proxy: Provides encrypted transmission, suitable for scenarios requiring secure transmission.
  • SOCKS Proxy: SOCKS proxy is a network protocol that supports various types of network services, including SOCKS4 and SOCKS5.

requests Library

# http/https
import requests

proxies = {
  'http': 'http://10.10.1.10:3128',  
  'https': 'https://10.10.1.10:1080',  
}

response = requests.get('http://example.org',  proxies=proxies)
print(response.text)

# socks4/5
import requests

proxies = {
  'http': 'socks5://user:password@host:port',
  'https': 'socks5://user:password@host:port',
}

response = requests.get('http://example.org',  proxies=proxies)
print(response.text)

aiohttp Library

# http/https
import aiohttp
import asyncio

async def fetch(session, url):
    async with session.get(url, proxy='http://10.10.1.10:3128')  as response:
        return await response.text()

async def main():
    async with aiohttp.ClientSession() as session:
        html = await fetch(session, 'http://example.org')  
        print(html)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

# socks4/5
import aiohttp
import asyncio
from aiohttp_socks import ProxyConnector

async def fetch(session, url):
    async with session.get(url) as response:
        return await response.text()

async def main():
    connector = ProxyConnector.from_url(socks_proxy_url)
    async with aiohttp.ClientSession(connector=connector) as session:
        async with session.post('https://api.example.com/data') as response:
            html = await response.text()
            print(html)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

cURL

<?php

// http/https
$url = 'http://example.org';  

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, 'http://10.10.1.10:3128');  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

echo $output;

// socks4/5
$url = 'http://example.org';  

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, 'socks5://10.10.1.10:1080'); // SOCKS5 proxy
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); // Specify the proxy type as SOCKS5
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

echo $output;

HttpClient

// http/https
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class HttpProxyExample {
    public static void main(String[] args) {
        String proxyHost = "proxy.example.com";
        int proxyPort = 8080;

        // Create HttpClient and set proxy
        CloseableHttpClient httpClient = HttpClients.custom()
                .setProxyConfiguration(proxyHost, proxyPort)
                .build();

        // Create HttpGet request
        HttpGet httpGet = new HttpGet("http://example.com");  

        try {
            // Execute request
            httpClient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

// socks4/5
import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.ProxySelectorRoutePlanner;

import java.net.InetSocketAddress;
import java.net.Proxy;

public class SocksProxyExample {
    public static void main(String[] args) {
        String proxyHost = "socksproxy.example.com";
        int proxyPort = 1080;

        // Create SOCKS proxy
        Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(proxyHost, proxyPort));

        // Create RoutePlanner
        ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(
                new HttpHost("http://example.com"),   proxy
        );

        // Create HttpClient and set SOCKS proxy
        CloseableHttpClient httpClient = HttpClients.custom()
                .setRoutePlanner(routePlanner)
                .build();

        // Create HttpGet request
        HttpGet httpGet = new HttpGet("http://example.com");  

        try {
            // Execute request
            httpClient.execute(httpGet);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}