Site icon Infinitricks

How to use proxy in python and javacript

how to use proxy in python and javascript

Hi, have you encountered any problems while accessing a website in your program? Alright, let’s discuss a topic, which is how to use a proxy in Python and JavaScript. Let’s go!

What is a proxy?


A proxy is a computer system or server that acts as a bridge between your device and the network. It serves as an intermediary, allowing you to hide your original IP address from the address you are trying to access. For example, if you access site x with IP address 182.1.1.1, site x will know your IP is 182.1.1.1. If you use a proxy, the process involves accessing site x through an intermediary server, so the site will recognize your IP as the proxy’s IP, like 49.2.2.2.

What are the uses of a proxy?


There are many uses for proxies, but this time I’ll give examples of common scenarios where I’ve encountered them, where many websites implement:

  1. Country selection: Not all foreign websites can be accessed from an Indonesian IP.
  2. Rate limiting: Some websites limit the number of requests you can make, e.g., not allowing more than 10 requests for information or reports per day.
  3. IP blocking due to suspicious activity that harms a website.

How to use a proxy in the Python programming language?
I’ll use the popular ‘requests’ library to make a URL call. Here’s how to use it:

import requests

url = 'http://example.com'  # Replace this with the URL you want to call

proxy_url = 'proxy_host:proxy_port'
proxy_user = 'username'
proxy_pass = 'password'

proxies = {
    'http': f'http://{proxy_user}:{proxy_pass}@{proxy_url}',
    'https': f'http://{proxy_user}:{proxy_pass}@{proxy_url}'
}

try:
    response = requests.get(url, proxies=proxies)

    if response.status_code == 200:
        print("Request successful!")
        print("Response content:")
        print(response.text)
    else:
        print(f"Request failed with status code: {response.status_code}")

except requests.exceptions.RequestException as e:
    print(f"Request error: {e}")

How to use a proxy in the JavaScript programming language?
For JavaScript, I’ll use the ‘axios’ library and the ‘tunnel’ library. You can install both via NPM. Here’s the code:

function getTunnelAgent(proxy_credentials) {
    let username = proxy_credentials.split('@')[0].split(':')[0];
    let password = proxy_credentials.split('@')[0].split(':')[1];
    let ip = proxy_credentials.split('@')[1].split(':')[0];
    let port = proxy_credentials.split('@')[1].split(':')[1];

    return tunnel.httpsOverHttp({
        proxy: {
            host: ip,
            port: port,
            proxyAuth: `${username}:${password}`,
        },
        rejectUnauthorized: false,
    });
}

let config = {
    headers: headers,
    timeout: 10000,
    httpsAgent: getTunnelAgent('proxy_username:proxy_password@proxy_host:proxy_port')
};

let response = await axios.get(url, config);

The above instructions explain how to use a proxy in both languages. This is useful when you need to access a website but face issues like the ones I mentioned earlier. That’s all from me; I hope this is helpful. Find other tips here. See you next time!

Written by:
@akhisyabab

Exit mobile version