Warning: include_once(/home/favoledo/robertozanardo.com/wp-content/sunrise.php): Failed to open stream: No such file or directory in /home/favoledo/robertozanardo.com/wp-includes/ms-settings.php on line 52

Warning: include_once(): Failed opening '/home/favoledo/robertozanardo.com/wp-content/sunrise.php' for inclusion (include_path='.:/opt/alt/php83/usr/share/pear:/opt/alt/php83/usr/share/php:/usr/share/pear:/usr/share/php') in /home/favoledo/robertozanardo.com/wp-includes/ms-settings.php on line 52
Semplice server locale in python 3 per test cgi e json con CORS abilitato e preflight - Odoo e Soluzioni Open Source

Semplice server locale in python 3 per test cgi e json con CORS abilitato e preflight

Grazie al supporto nativo di pyton, possiamo eseguire un semplice server locale con il comando:

python -m http.server --cgi

A questo punto è sufficiente creare un qualsiasi file in python tipo questo, che possiamo chiamare index.py (e preferibilmente salvare in una cartella cgi-bin, direttamente nella posizione dalla quale abbiamo lanciato il comando precedente):

#!/usr/bin/env python3
print("Content-type: text/html")
print("CONTENUTO HTML")

E a questo punto possiamo visitare dal browser:
http://127.0.0.1:8000/cgi-bin/index.py

Molto utile per test locali di chiamate cgi e json.
Potete approfondire poi con la libreria cgi di python: https://docs.python.org/3/library/cgi.html

Grazie a questo articolo di Zemian Deng, che ho solo adattato, testato e tradotto.

Eseguendo successive ricerche e avendo la necessità di abilitare anche le richieste CORS ho trovato questa configurazione ideale per lanciare un server locale con tutto abilitato, modificato per supportare l’interpretazione CGI ed accettare diverse condizioni di ‘preflight’:

#!/usr/bin/env python3
from http.server import HTTPServer, SimpleHTTPRequestHandler, CGIHTTPRequestHandler, test
import sys
import cgi
from json import dumps

class CORSRequestHandler (CGIHTTPRequestHandler):
    def end_headers (self):
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header("Access-Control-Allow-Methods", "GET,POST,OPTIONS")
        self.send_header("Access-Control-Allow-Headers", "x-api-key,Content-Type,Accept, X-Debug-Mode,Origin, X-Requested-With")
        CGIHTTPRequestHandler.end_headers(self)

    def send_dict_response(self, d):
      """ Sends a dictionary (JSON) back to the client """
      self.wfile.write(bytes(dumps(d), "utf8"))

    def do_OPTIONS(self):
      self.send_response(200)
      #self._send_cors_headers()
      self.end_headers()

    def do_GET(self):
      self.send_response(200)
      #self._send_cors_headers()
      self.end_headers()

      response = {}
      response["status"] = "OK"
      self.send_dict_response(response)

    def do_POST(self):
      self.send_response(200)
      #self._send_cors_headers()
      self.send_header("Content-Type", "application/json")
      self.end_headers()

      dataLength = int(self.headers["Content-Length"])
      data = self.rfile.read(dataLength)

      print(data)

      response = {}
      response["status"] = "OK"
      self.send_dict_response(response)

if __name__ == '__main__':
    test(CORSRequestHandler, HTTPServer, port=int(sys.argv[1]) if len(sys.argv) > 1 else 8000)

Lascia un commento

kolaybet giriş
kolaybet
betgaranti
bettilt
kolaybet
betpark
betgaranti
betpark
betgaranti
kolaybet
kolaybet
betpark
betpark
hellocasino
betgaranti
Kolaybet
bettilt
betpark
betpark
Kolaybet
Kolaybet Giriş
Kolaybet
betpark
betpark
kolaybet
betpark
betgaranti
kolaybet giriş
kolaybet
Kolaybet Giriş
Kolaybet
Kolaybet Telegram
Kolaybet Giriş
betpark
betpark
holiganbet
Kolaybet
Kolaybet
Betgaranti
matbet güncel giriş
matbet güncel giriş
matbet güncel giriş
matbet güncel giriş
betgaranti
betgaranti
betgaranti
sahabet
sahabet
kolaybet
kolaybet
sahabet
Hititbet
Hititbet Giriş
sahabet
Hititbet Güncel Giriş
vdcasino
onwin
onwin
hititbet
betgaranti
Hellocasino
Hello Casino
Hititbet
Hititbet
Hititbet Güncel Giriş
hititbet
vdcasino
onwin
onwin
grandpashabet
betpark
betgaranti
hititbet
grandpashabet
bettilt
sahabet
sahabet
sahabet
onwin
onwin
hititbet giriş
hititbet
hititbet giriş
hititbet mobil giriş
hititbet güncel giriş
vdcasino
grandpashabet
kolaybet
kolaybet
betpark
Kolaybet Giriş
Kolaybet Güncel Giriş
betgaranti
betgaranti giriş
betgaranti 2026
betpark
betgaranti