Home>

Here is my primitive server code:

Import Http.Server
Import SocketServer.
Port= 8080.
Handler= http.server.simplehtttprequesthandler
With SocketServer.tcPServer ((", Port), Handler) AS httpd:
    Print ("SERVING AT Port", Port)
    httpd.serve_forever ()

How to connect a JSON file to it so that data access was not by http: //localhost: 8080 /db.json, and by http: //localhost: 8080 /data

json file:

{
    "Data": {
        "Barcode": "1234587867678",
        "WEIGHT": "3",
        "VOLUME": "0.9",
        "LENGTH_": "8",
        "Width": "2",
        HEIGHT: "9",
        "UNIQUEINDEX": "555"
    }
}
  • Answer # 1
    Import JSON
    Import http.server
    Import SocketServer.
    Port= 8080.
    Class Handler (http.server.simplehttprequesthandler):
        DEF Do_get (Self):
            If self.path== '/Data':
                Self.Send_Response (200)
                Self.Send_Header ("Content-Type", "Application /JSON")
                Self.End_headers ()
                With Open ('Data.json') AS JSON_DATA:
                    JSON_DATA= JSON.LOAD (JSON_DATA)
                Self.wfile.Write (JSON_DATA), "UTF8"))
                Return.
            Return Super (). do_get ()
    With SocketServer.tcPServer ((", Port), Handler) AS httpd:
        Print ("SERVING AT Port", Port)
        httpd.serve_forever ()