0

A simple HTTP request in Python will succeed from some pcs and not others. The pcs are on different networks

python version = 3.10.10 requests==2.28.2

The request can be as simple as below but replace the API URL with the one that you are having trouble with.

import requests
print(requests.get("https://api.github.com"),)

No error is produced. There is no sign of activity on the http server.

If I add a timeout such as

import requests
print(requests.get("https://api.github.com", timeout=1))

the request will complete successfully. However, it is clear from testing with various timeout values that the request is only being made after the timeout ends.

This creates a problem for performance testing.

John Curry
  • 392
  • 3
  • 12

1 Answers1

0

The answer turned out to be that the pc with the issue was on a network that was defaulting to IPV6 requests.

After the timeout has elapsed, it tries to connect using IPV4 and is successful.

When I force IPV4, the problem is solved.

import requests
requests.packages.urllib3.util.connection.HAS_IPV6 = False
print(requests.get("https://api.github.com"),)

This issue from a Raspberry Pi gave me the clue.

Python requests.get() only responds if adding a time out

They had to force IPV4 and did it through a router setting.

Can also do it in code as above (reference below). Putting it here so as pc/server users who encounter this issue can find a clear answer.

Force requests to use IPv4 / IPv6

John Curry
  • 392
  • 3
  • 12