user3638166
user3638166

Reputation: 11

Routing Flask application API GET requests through pythonanywhere proxy

I've been stuck for a couple of days now trying to get a little football fixtures website hosted on pythonanywhere.com as a first personal project using FLASK

The issue I seem to have run into is that I need to route API calls through the pythonanywhere proxy (proxy.server:3128) but I've no idea how to configure this (I'm a beginner tbh)

Any help to point me in the right direction would be much appreciated

Some sample code below as an example of a request I'm trying to make (these work fine when hosted locally, but pythonanywhere requires the proxy routing for http requests)

from flask import Flask, request
import http.client

connection = http.client.HTTPConnection('api.football-data.org')

def getCompetitions():
    print ("running getCompetitions")
    connection.request('GET', '/v2/competitions/', None, headers )
    response = json.loads(connection.getresponse().read().decode())
    return response

competitions = getCompetitions()

Upvotes: 0

Views: 246

Answers (1)

user3638166
user3638166

Reputation: 11

Found it! Actually not too bad, just I'm a noob!

I needed to use the .set_tunnel function related to python's http.client function to do the routing through the proxy Documentation here: https://docs.python.org/3/library/http.client.html

Usage for this example:

connection = http.client.HTTPSConnection("proxy.server", 3128)
connection.set_tunnel("api.football-data.org")

Hopefully this helps someone!

Upvotes: 1

Related Questions