machine-learning
machine-learning copied to clipboard
External 'requests.post' not working
When issuing a requests.post
from outside the corresponding docker unit tests, we get a 500 error. Specifically, the following was tested from a vagrant virtual machine:
import requests
username = 'xxxxxxxxxxxx'
password = 'xxxxxxxxxxxx'
endpoint = 'xxxxxxxxxxx'
port = 8585
headers = {
'Content-Type': 'application/json',
}
login = requests.post(
'https://{}:{}/login'.format(endpoint, port),
headers=headers,
data={'user[login]': username, 'user[password]': password},
verify=False
)
token = login.json
print('token: {}'.format(repr(token)))
Then, we get the following error:
root@ip-172-31-47-47:/home/ubuntu/ist-652# python3 test.py
/usr/lib/python3/dist-packages/urllib3/connectionpool.py:794: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.org/en/latest/security.html
InsecureRequestWarning)
token: <bound method Response.json of <Response [500]>>
Note: we used the verify = False
, since the corresponding application implements a self signed certificate. Additionally, the application redirects all http
requests to https
.
The above 500
error is achieved when the correct port rules are configured to be open. Otherwise, variations of different http errors are returned. However, the former http error indicates either there was a problem with the nginx-api reverse proxy, or the corresponding gunicorn-api webserver.
Part of our earlier confusion was due to the fact that the reverse proxy for the backend api, was configured on port 9595
. However, we were making a request the frontend api at port 8585
With the adjusted port 9595
, as well as using token = login.json()
, we receive a timeout
:
root@ubuntu-xenial:/vagrant# python3 test.py
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 137, in _new_conn
(self.host, self.port), self.timeout, **extra_kw)
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 91, in create_connection
raise err
File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 81, in create_connection
sock.connect(sa)
socket.timeout: timed out
Our problems need to be further investigated based on the following:
root@ubuntu-xenial:/vagrant# nmap -sS -p8585 xx.xx.xx.xx
Starting Nmap 7.01 ( https://nmap.org ) at 2018-09-29 22:01 UTC
Nmap scan report for xx.xx.xx.xx (xx.xx.xx.xx)
Host is up (0.0020s latency).
PORT STATE SERVICE
8585/tcp open unknown
Nmap done: 1 IP address (1 host up) scanned in 0.67 seconds
root@ubuntu-xenial:/vagrant#
root@ubuntu-xenial:/vagrant#
root@ubuntu-xenial:/vagrant#
root@ubuntu-xenial:/vagrant# nmap -sS -p9595 xx.xx.xx.xx
Starting Nmap 7.01 ( https://nmap.org ) at 2018-09-29 22:01 UTC
Nmap scan report for xx.xx.xx.xx (xx.xx.xx.xx)
Host is up (0.00072s latency).
PORT STATE SERVICE
9595/tcp filtered pds
Nmap done: 1 IP address (1 host up) scanned in 0.77 seconds
We sent a POST
request from the nginx-api
, to the webserver-api
container:
>>> import requests
>>> requests.post('http://webserver-api:6001/login', headers={'Content-Type': 'application/json'}, data={'user[login]': 'jeff1evesque', 'user[password]': 'password123'}, verify=False)
<Response [400]>
>>> requests.post('http://webserver-api:6001/login', headers={'Content-Type': 'application/json'}, data={'user[login]': 'jeff1evesque', 'user[password]': 'password123'})
<Response [400]>
Posting from the nginx-api
, to the webserver-api
container, without headers
:
>>> requests.post('http://webserver-api:6001/login', data={'user[login]': 'jeff1evesque', 'user[password]': 'password123'})
<Response [500]>
Even after executing the above, no logs entries exists:
root@webserver-api:/var/log/webserver# ls -l
total 0
-rw-r--r-- 1 root root 0 Jun 21 05:54 flask.log
root@webserver-api:/var/log/webserver# cd /var/machine-learning/log
root@webserver-api:/var/machine-learning/log# ls -l
total 12
-rw-r--r-- 1 root root 117 Sep 12 20:54 __init__.py
-rw-r--r-- 1 root root 5670 Sep 12 20:54 logger.py
We'll need to fix, and ensure that some logging mechanism is capable of capturing errors associated with our earlier 500
errors. However, before proceeding, we'll likely need to fix #3287. This requirement will be dictated by the minimum requirement of matching the puppet-agent with xenial, rather than trusty. Then, additional updates associated with python3 + pip3 will be non-required benefits. However, if python3 is implemented, we'll likely need to refactor python code. For example, iteritems
will need to be refactored to items
.