Simple Python Webserver to save file

前端 未结 2 742
情深已故
情深已故 2020-12-15 21:39

I\'m trying to make a simple python webserver to save text that is Posted to a file called store.json which is in the same folder as the python scr

2条回答
  •  一个人的身影
    2020-12-15 21:51

    Here's the general idea:

    from os import curdir
    from os.path import join as pjoin
    
    from http.server import BaseHTTPRequestHandler, HTTPServer
    
    class StoreHandler(BaseHTTPRequestHandler):
        store_path = pjoin(curdir, 'store.json')
    
        def do_GET(self):
            if self.path == '/store.json':
                with open(self.store_path) as fh:
                    self.send_response(200)
                    self.send_header('Content-type', 'text/json')
                    self.end_headers()
                    self.wfile.write(fh.read().encode())
    
        def do_POST(self):
            if self.path == '/store.json':
                length = self.headers['content-length']
                data = self.rfile.read(int(length))
    
                with open(self.store_path, 'w') as fh:
                    fh.write(data.decode())
    
                self.send_response(200)
    
    
    server = HTTPServer(('', 8080), StoreHandler)
    server.serve_forever()
    
    $ curl -X POST --data "one two three four" localhost:8080/store.json
    $ curl -X GET localhost:8080/store.json    
    one two three four%
    

提交回复
热议问题