DEV Community

loading...

Testing a website from different countries using Python 3 and Proxy Orbit

fprime profile image Frankie ・3 min read

It can be tricky to test web applications from different countries. Services exist that make it easier, but they often cost money for the really useful features. In this post I am going to go over how we can automate the testing of a web application in different regions using free web proxies. To do this, I will be using Python 3 and Proxy Orbit.

To start out, I have created a very basic demo application that detects the location of incoming users and returns a page that looks like the image below.

The flag and text will change depending on the IP address that the user is using at the time.

The code for this is below. It uses Python, Flask, and the geoip2 library. It also requires that you have an mmdb file for IP address lookup named "ip_country_db.mmdb"

app.py

import geoip2.database as geoip
from flask import Flask, render_template, request

app = Flask(__name__)

@app.route("/")
def index():
    ip_addr = request.remote_addr
    reader = geoip.Reader("ip_country_db.mmdb")
    country = reader.country(ip_addr)
    return render_template("index.html", country=country, name=country.country.names['en'])

if __name__ == "__main__":
    app.run(port=8080, host="0.0.0.0")

index.html

<html>
    <head>
        <title>Location Greeter</title>
    </head>
    <body>
        <div id="greeting">
            <img src="https://www.countryflags.io/{{ country.country.iso_code }}/flat/64.png">
            <div id="words">
                Hello {{ name }}!
            </div>
        </div>
    </body>
</html>

Now, getting to the fun stuff. To write the tests we are going to create
a Python script that loads the web page and checks for specific text on the page associated with the location being tested.

To start off let's create a basic Python script that loads a web page

import os                         
import requests    

url = os.getenv("TEST_URL")                                                                                                                                    

resp = requests.get(url)                                                                                                                                          

print(resp.content)  

This script will simply load a web page located in the TEST_URL env variable and print its contents.

Now to write the code that actually tests the contents of the webpage for the correct location name.

import os
import requests

url = os.getenv("TEST_URL")

countries = [
    {"code": "US", "name": "United States"},
    {"code": "GB", "name": "United Kingdom"},
    {"code": "CN", "name": "China"},
]

for country in countries:
    resp = requests.get(url)
    if country['name'] not in resp.content.decode():
        raise Exception(f"{country['name']} not on webpage")

This code will obviously fail because each request will be made from the same IP address. To fix this we will be using Proxy Orbit (https://proxyorbit.com). Proxy Orbit is a rotating proxy API. Each request will give us a new proxy that we can use in our script. We can specify where the proxy is located using the API query arguments.

import os    
import requests    

url = os.getenv("TEST_URL")    
proxy_orbit_url = "https://api.proxyorbit.com/v1/?token=Iut1LQeCvN7bxHRkplubawI75qGiWxiXrKR1ARflDKA&location={country}"                              
countries = [                                                                                                                                                  
    {"code": "US", "name": "United States"},                                                                                                                   
    {"code": "GB", "name": "United Kingdom"},                                                                                                                  
    {"code": "CN", "name": "China"},                                                                                                                        
]                                                                                                                                                           

for country in countries:                                                                                                                                   
    presp = requests.get(proxy_orbit_url.format(country=country["code"]))                                                                                   
    if presp.status_code != 200:                                                                                                                            
        raise Exception("Could not get proxy")                                                                                                              
    proxy_json = presp.json()    
    pcurl = proxy_json["curl"]    
    resp = requests.get(url, proxies={"http": pcurl})    
    if country["name"] not in resp.content.decode():    
        print(country['name'], "Failed")                        
    else:                                                       
        print(country["name"], "Passed")   

We didn't have to add much to integrate proxy support. We added a line in the beginning of the script to specify our Proxy Orbit API URL that will be used later. We also added a country string variable that will be modified later.

In the for loop we first make a request to the Proxy Orbit API formatting in the country code to get a new proxy in the region we need. Then we add the proxy to the request that is made to the URL we are trying to test.

The script will then print whether the country name was found on the web page or not when attempting to load the page with the proxy.

More countries can be added by adding new country dictionaries to the countries list.

Discussion (0)

pic
Editor guide