Search code examples
pythontelegram-botpythonanywhereurllib3

Can PythonAnywhere Free Account code call outside https Apis?


I'm writing a telegram bot to serve a set of relational card games, and used a blend of ChatGPT output with what I found at https://blog.pythonanywhere.com/148/.

I can get the bot to start, which is great. The issue is that when I call my website www.relationalgames.com/cards/deck?Papo a Papo, I get the error:

File "/usr/local/lib/python3.10/site-packages/urllib3/util/retry.py", line 592, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.relationalgames.com', port=443): Max retries exceeded with url: /cards?deck=Papo%20a%20Papo (Caused by NewCo nnectionError('<urllib3.connection.HTTPSConnection object at 0x7f46da0873a0>: Failed to establish a new connection: [Errno 111] Connection refused'))

I wonder if this is some limitation of PythonAnywhere free account, or if I am doing something wrong...Any ideas on how to overcome this?

Here is my current code

import telepot
import json
import random
import urllib3
import time

# You can leave this bit out if you're using a paid PythonAnywhere account
proxy_url = "http://proxy.server:3128"
telepot.api._pools = {
    'default': urllib3.ProxyManager(proxy_url=proxy_url, num_pools=3, maxsize=10, retries=False, timeout=30),
}
telepot.api._onetime_pool_spec = (urllib3.ProxyManager, dict(proxy_url=proxy_url, num_pools=1, maxsize=1, retries=False, timeout=30))
# end of the stuff that's only needed for free accounts

http = urllib3.PoolManager(timeout=60)

def handle(msg):
    content_type, chat_type, chat_id = telepot.glance(msg)
    if content_type == 'text':
        if msg['text'] == '/start':
            bot.sendMessage(chat_id, "Enter the name of a deck:")
        else:
            #try:
                deck_name = msg['text']
                response = http.request('GET', f'https://www.relationalgames.com/cards?deck={deck_name}') # requests.get(f'https://www.relationalgames.com/cards?deck={deck_name}')
                print(response)
                playable_deck = json.loads(response.text)
                random.shuffle(playable_deck)

                while len(playable_deck) > 0:
                    card = playable_deck.pop()
                    if 'url' in card:
                        bot.sendMessage(chat_id, card['url'])
                    else:
                        bot.sendMessage(chat_id, card['cardText'])

                    if len(playable_deck) == 0:
                        bot.sendMessage(chat_id, "Do you want to restart the game? (yes or no)")
                        break

                    bot.sendMessage(chat_id, "Press any key for next card")
                    bot.getUpdates()
            #except:
             #   bot.sendMessage(chat_id, "Error processing your request")

TOKEN = 'My Token'
bot = telepot.Bot(TOKEN)
bot.message_loop(handle)

print ('Listening ...')

# Keep the program running.
while 1:
    time.sleep(10)

Solution

  • Free accounts on PythonAnywhere have restricted internet access to allowlisted public APIs (HTTP(s) only). You can request an allowlist addition if the site in question exposes public API with an official documentation.