Python Forum
Error getting HTTP 200 response with requests.get while wget works fine - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Networking (https://python-forum.io/forum-12.html)
+--- Thread: Error getting HTTP 200 response with requests.get while wget works fine (/thread-5731.html)



Error getting HTTP 200 response with requests.get while wget works fine - sonicblind - Oct-18-2017

Hi,

I am struggling with smc-python library - frontend to Stonegate API.

To create a session with the API I should simply use:
session.login(url='http://1.1.1.1:8082', api_key='xxxxxxxxxxxxxxxxx')
When I run a script with this login I receive Exception:

Output:
user@script:~$ ./smc_api.py Traceback (most recent call last): File "./smc_api.py", line 51, in <module> xxxxxxx File "/usr/local/lib/python3.5/dist-packages/smc/api/session.py", line 214, in login self._api_version = get_api_version(url, api_version, timeout, verify) File "/usr/local/lib/python3.5/dist-packages/smc/api/session.py", line 381, in get_api_version versions = available_api_versions(base_url, timeout, verify) File "/usr/local/lib/python3.5/dist-packages/smc/api/session.py", line 368, in available_api_versions 'Status code received %s. Reason: %s' % (r.status_code, r.reason)) smc.api.exceptions.SMCConnectionError: Invalid status received while getting entry points from SMC. Status code received 404. Reason: Not Found
When I trace the function available_api_versions it basically does a request as follows:
def get_entry_points(base_url, timeout=10, verify=True):
    try:
        r = requests.get('%s/api' % (base_url), timeout=timeout, verify=verify)
        .......
So I tried to execute this function directly and wrote this small test script:
try:
	r = requests.get('http://1.1.1.1:8082/api')
	if r.status_code == 200:
		print('success')
	else:
		raise Exception('Status code received %s. Reason: %s' % (r.status_code, r.reason))
except Exception as ex:
	print("An exception of type {0} occurred. Arguments:\n{1!r}".format(type(ex).__name__, ex.args))
And the result:
Output:
user@server:~$ ./test.py An exception of type Exception occurred. Arguments: ('Status code received 404. Reason: Not Found',)
So my issue is that wget is actually able to get a response from API while the requests.get is not.

WORKS: wget -qO- http://1.1.1.1:8082/api
DOES NOT WORK: r = requests.get('http://1.1.1.1:8082/api')

Proxy settings are not an issue, both requests are able to reach the API.
But while the wget gets HTTP/200 response with actual data, requests.get receives HTTP/404 not found.

What am I missing here? What is different? I would assume both wget and requests.get should behave the same way when exactly the same URL is provided as parameter.

Any advice very welcome!
Thanks.

One small correction. I've made a typo in the function name.
It should be available_api_versions instead of get_entry_points.
But it does not matter anyway as both functions are doing the same request:
r = requests.get('%s/api' % base_url, timeout=timeout, verify=verify)

!!!MY MISTAKE!!!
It was indeed the problem with Proxy. requests.get was using the system proxy and the HTTP/404 was actually a reply from the proxy.
Problem solved :-)