Ashutosh Kumar
Ashutosh Kumar

Reputation: 59

How to force gevent WSGIServer to listen both HTTP and HTTPS

I am able to access the application using HTTPS but how can i redirect it to listen http as well after converting it to https.

When i am trying to access it using http, i am getting following error.

    ssl.SSLError: [SSL: SSLV3_ALERT_CERTIFICATE_UNKNOWN] sslv3 alert certificate unknown (_ssl.c:1056)
2020-03-30T15:58:35Z <Greenlet at 0x1d7ca90ce18: _handle_and_close_when_done(<bound method StreamServer.wrap_socket_and_handle , <bound method StreamServer.do_close of <WSGIServer, (<gevent._socket3.socket [closed]  object, fd=-1, )> failed with SSLError

My run.py

from search_app import app
from gevent.pywsgi import WSGIServer
import os


if __name__ == '__main__':
    http_server = WSGIServer(('127.0.0.1', 5000), app, keyfile=f'{os.path.dirname(app.instance_path)}\\private.key', certfile=f'{os.path.dirname(app.instance_path)}\\xyz.crt')
    http_server.serve_forever()

Upvotes: 3

Views: 1409

Answers (1)

Abolfazl Rastgou
Abolfazl Rastgou

Reputation: 832

try this:

from gevent.pywsgi import WSGIServer
from ssl import PROTOCOL_SSLv23
from gevent import sleep as gevent_sleep, ssl

Flask app code here

if __name__ == "__main__":

    http_server = WSGIServer(("0.0.0.0", 80), app, log=None, error_log=None, backlog=None)
    http_server.start()

    https_server = WSGIServer(("0.0.0.0", 443), app, keyfile='./apps/server.key', certfile='./apps/server.crt',
                              log=None, error_log=None, backlog=None, ssl_version=PROTOCOL_SSLv23,
                              do_handshake_on_connect=False)

    try:
        https_server.start()
    except (ssl.SSLError,ssl.SSLEOFError) as err:
        if err.args[1].find("sslv3 alert") == -1:
            raise


    while True:
        gevent_sleep(60)

Upvotes: 0

Related Questions