Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
Send XMLHttpRequest POST to python cgi

I am sending a text string from javascript to a cgi python3 program. User initiating the send with a button.

With the simplified code I made below the server seems to acknowledge the POST but the cgi does not run. (no output is shown). However if I refresh the page where my button is then the output of the cgi program is now shown on stderr. Strange behaviour.

Any pointers?

I am using a python cgi server.

python3 -m http.server --cgi 8000

the html file:

<html>
<head>
<script>
function post_data(){
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "/cgi-bin/cgi.py");
    xhr.setRequestHeader('Content-Type', 'text/html');
    xhr.send("hello world");
    
    xhr.onload = function() {
        console.log(`Loaded: ${xhr.status} ${xhr.response}`);
    };
    xhr.onerror = function() {
        console.log(`Network Error`);
    };
}
</script>
<body>
<button type="button" class="btn btn-primary" onclick="post_data()">Send</button>
</body></html>

The python cgi:

#!/usr/bin/python3
import sys
sys.stderr.write(sys.stdin.read())
print("Content-Type: text/html\n\n")
print("<html><body>ok</body></html>")

The output once the button is clicked:

Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
127.0.0.1 - - [15/Oct/2021 17:03:38] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [15/Oct/2021 17:03:39] "POST /cgi-bin/cgi.py HTTP/1.1" 200 -

after refreshing the page this additional output comes:

hello world127.0.0.1 - - [15/Oct/2021 17:04:03] "GET / HTTP/1.1" 304 -
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda