Forums

Time-out while trying to do a UDP connection to an external server

Hi there,

While migrating my scripts to PythonAnywhere I'm trying to solve an issue regarding UDP connections. I constantly have time-outs while connecting to an external servers via UDP. For example:

[2024-04-01 13:32:51,104] DEBUG in scraper: Scraping udp://....lu:80/announce for 72 hashes
[2024-04-01 13:32:56,112] DEBUG in scraper: udp://....lu:80/announce timed out, skipped

The same Python code works fine on my previous hosting and locally.

I understand that running a server that accepts incoming UDP connections are not supported here, but I'm doing the opposite. So I'm wondering if outbound UDP socket connections are not supported at all on PythoAnywhere. This would be very unfortunate. ;(

I'm using the Python socket module to initiate the connection:

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(UDP_TIMEOUT)
conn = (socket.gethostbyname(parsed_tracker.hostname), parsed_tracker.port)

...

Thanks for any information!

Yes, we do support outbound UDP in paid accounts. Is it possible that the server you're trying to connect to is blocking the incoming UDP packets when they come from cloud environments like PythonAnywhere? That's the most likely cause of issues like that.

Hey Giles, thanks for the reply! I cannot exclude servers are blocking my requests. My previous hosting was Dreamhost and I had an occasional time-outs, but nothing big.

What I find strange is that right now suddenly I have all the servers (about 30) which regularly time out connections. Anyway, given that PythonAnywhere supports UDP I have to find out the root cause elsewhere.

Let us know if you find out.

In the end I side-stepped the issue by not doing UDP calls from the server and let clients do the work. After monitoring the issue for a while I noticed that sometimes the system was able to connect via UDP (I got something like 20/30 out of 300 successful "scrape" requests per task) so this definitely depends on certain tracker servers that most likely blacklisted PythonAnywhere IP's.

Thanks! That's useful to know.