Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

329
Visualizações
Flask SocketIO + Apache

I have successfully created a Flask-SocketIO application that runs on eventlet server. Then I moved the webpage to Apache web server. I just deleted the part of myapp.py where template was rendered and placed the index.html file in Apache's /www/html directory. Here is the code:

myapp.py:

from flask import Flask
from flask_socketio import SocketIO, emit

import eventlet
eventlet.monkey_patch()

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
io = SocketIO(app, cors_allowed_origins="http://localhost")

clients = []

@io.on('connected')
def connected():
    clients.append(request.sid)
    print("client connected")
    print(request.sid)

@io.on('disconnect')
def disconnect():
    clients.remove(request.sid)
    print("client disconnected")
    print(request.sid)

if __name__ == '__main__':
    io.run(app, host='localhost', port=5000)

index.html:

<html>
<br>

<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
   <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js" integrity="sha256-yr4fRk/GU1ehYJPAs8P4JlTgu0Hdsp4ZKrx8bDEDC3I=" crossorigin="anonymous"></script>
</head>

<br>

<body>

   <h2>HELLO</h2>

   <script>

      $("document").ready(function(){

         var socket = io.connect('http://localhost:5000');

         socket.on('connect', function() {
            socket.emit('connected');
         });

      });

   </script>

</body>

</html>

Everything works just fine. But then I read Miguel's answer to this question: Using eventlet to manage socketio in Flask, which says that using Apache's web server is not a good idea. I do not understand very well how web servers work. Is what I did a wrong thing to do? The answer also says that Apache does not support web sockets. How come my application works at all, then? I'll be very thankful if somebody can explain this matter to me.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Your example works because you are only serving your HTML file through Apache, the Flask-SocketIO server is still running by itself on port 5000.

What is currently difficult/impossible to do is to make the Socket.IO connections pass through Apache and then on to the server, which is what most people prefer.

You are really running two web servers, Apache on ports 80/443 for your HTML and Flask-SocketIO on port 5000 for your Python logic.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda