0

I’m having trouble implementing SSL to my aiohttp TCPSite connection. I have a VPS set up with Ubuntu and Apache. My config files are set to enable CORS and SSL is active on the server. When I go to the actual resource ie https://irisslivedev.com:448 there are no problems, further leading me to believe that it’s a python async server problem.

On the frontend I’m getting a 404 error for the resource, which can only mean the TCPSite server isn’t listening on the port... right? Here’s the code for the server:

sslcontext = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
sslcontext.load_cert_chain('cert2.pem', 'privkey2.pem')


app = web.Application()

sio = socketio.AsyncServer(cors_allowed_origins='*')


sio.attach(app)

SOCKET_PORT = 448

SERVER_URL = "irisslivedev.com"


async def runSIOWebApp(port):                 
    # We bind our aiohttp endpoint to our app
    # router
    #web.run_app(self.app, host=self.server, port=self.port)
    logger.log_il("Initializing Web App Runner...")
    runner = web.AppRunner(app)
    try: 
        logger.log_il("Starting Web App Runner on %s:%s..." % (SERVER_URL,port))
        await runner.setup()
        site = web.TCPSite(runner, SERVER_URL, port, ssl_context=sslcontext)
        logger.log_il("Starting Site...")
        await site.start()
        while (True):
            await asyncio.sleep(1)
    except Exception as e:
        logger.log_il("Could not start SIO Web App. %s " % e)
        traceback.print_exc(file=sys.stdout)
    finally: 
        logger.log_il("Closing Web App Runner...")
        await runner.cleanup()

When I run a stacktrace this is the output:

Traceback (most recent call last):
  File "il_receiver.py", line 198, in runSIOWebApp
    await site.start()
  File "C:\Users\jose\Python\lib\site-packages\aiohttp\web_runner.py", line 100, in start
    self._server = await loop.create_server(  # type: ignore
  File "C:\Users\jose\Python\lib\asyncio\base_events.py", line 1459, in create_server
    raise OSError(err.errno, 'error while attempting '
OSError: [Errno 10049] error while attempting to bind on address ('165.22.39.188', 443): the requested address is not valid in its context

I’m really not sure where to go from here, I’ve scoured all the resources I can find and similar answers. From what I understand, the error arises when you attempt to bind to a socket not on the local machine, or when one is already in use.

Here's my config:

``

    ServerAdmin [email]
    DocumentRoot /var/www/165.22.39.188/html
    ServerName 165.22.39.188

    Header always set Access-Control-Allow-Origin "https://irisslivedev.com"
    Header always set Access-Control-Allow-Methods "*"
    Header always set Access-Control-Max-Age "1000"
    Header always set Access-Control-Allow-Headers "*"
    Header always set Access-Control-Allow-Credentials true

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

  ServerAlias irisslivedev.com
  Include /etc/letsencrypt/options-ssl-apache.conf
  ServerAlias www.irisslivedev.com
  SSLCertificateFile /cert.pem
  SSLCertificateKeyFile /pkey.pem
 </VirtualHost>
 </IfModule>

`` If there’s any way I can make this question more clear, please let me know.

MjBVala
  • 137
  • 3
  • 9
  • 1
    Two potential problems. First of all, in your Python code, better to use `0.0.0.0` as your bind address. Do not use your hostname, or your IP address. Second, the error indicates your Python server is trying to bind to port 443, but I assume that must be the port where nginx is listening? – Miguel Grinberg Mar 03 '20 at 12:15
  • Thank you, the 0.0.0.0 wasn't the exact problem at the time, although It did help me solve my immediate problem and likely a future one as well! As for the port, It was an older stacktrace before i switched ports. – MjBVala Mar 03 '20 at 20:16

0 Answers0