gateway_http_uplink.py 1.92 KB
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Script to be executed.
Open a http server with function serve_forever(port)

Created on Wed Jun  9 10:56:11 2021

@author: Georges de Massol
"""



from http.server import HTTPServer, BaseHTTPRequestHandler
import urllib.parse
import gateway
import test_gateway
import gat
ip = 'localhost'

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.end_headers()
        self.wfile.write(b'Serveur uplink en marche!')
        print("do_get")

    def do_POST(self):
        print("do_post00")

        # ↓↓↓ Lecture de la dernière valeur de GNU Radio ↓↓↓
        # la lecture est faite le plus tôt possible pour éviter tout parasitage par un autre appareil.
        
        # Tn : échantillon n
        Tn = 123456
        # ↑↑↑ Lecture de la dernière valeur de GNU Radio ↑↑↑
        content_length = int(self.headers['Content-Length'])
        body = self.rfile.read(content_length)
        self.send_response(200)
        self.end_headers()
        print(urllib.parse.urlparse(self.path))
        print("do_post0")
        # print(urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query))
        event = urllib.parse.parse_qs(urllib.parse.urlparse(self.path).query).get('event', None)[0]
        print("do_post1")
        gat.postcompute(event, body, Tn)
        print("do_post2")

def serve_forever(port = 6666):
    """
    Function executed permanently. 
    Do not run code after this function.

    Parameters
    ----------
    port : int, optional
        Port for the server. The default is 6666.

    Returns
    -------
    None.

    """
    print("d1")

    httpd = HTTPServer((ip, port), SimpleHTTPRequestHandler)
    print("d2")
    print("http server for uplink started on {}:{}\n".format(ip, port))
    print("d3")
    httpd.serve_forever()
    print("d4")


#do not add code then
serve_forever()