Home > database >  Trying to send a POST request to a Flask app, SSL error occurs
Trying to send a POST request to a Flask app, SSL error occurs

Time:06-16

When I send a post request to my flask app, I get an ssl error:

  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
    httplib_response = self._make_request(
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 382, in _make_request
    self._validate_conn(conn)
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 1010, in _validate_conn
    conn.connect()
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 411, in connect
    self.sock = ssl_wrap_socket(
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\ssl_.py", line 428, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\ssl_.py", line 472, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 500, in wrap_socket        
    return self.sslsocket_class._create(
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1040, in _create
    self.do_handshake()
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1309, in do_handshake      
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1124)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
    retries = retries.increment(
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\retry.py", line 573, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='localhost', port=5000): Max retries exceeded with url: /predict (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1124)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 3, in <module>
    resp = requests.post("https://localhost:5000/predict", files={'file': open('mel.jpg', 'rb')})
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 119, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 61, 
in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "C:\Userinfo\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='localhost', port=5000): Max retries exceeded with url: /predict (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1124)')))

This is the code that I use to send the request:

import requests

resp = requests.post("https://localhost:5000/predict", files={'file': open('mel.jpg', 'rb')})
print(resp.text)

It does not seem that the problem is with the code, as it should work fine and the traceback does not seem to indicate anything in the code. What could the problem be?

CodePudding user response:

Found the solution to my problem! The problem, I think, is due to the code used to sent the request. I was sending using HTTPS when I didn't have an SSL certificate for my server, so I was not able to get any response. It now works like a charm :)

  • Related