from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
import nodeVariables
import os
import json
class DBServer(BaseHTTPRequestHandler):
    
    def do_POST(self):
        url = urlparse(self.path)
        path = url.path

        if (path == '/create'):
            length = int(self.headers.get('content-length'))
            field_data = self.rfile.read(length)
            entry = json.loads(field_data.decode('utf-8'))

            fp = open('data.json', 'r')
            read = json.load(fp)
            read.append({entry['key']:entry['value']})
            fp.close()

            f = open('data.json', 'w')
            json.dump(read, f)
            f.close()

            res = { "error": { "code": 201, "message": "User Added succesfully" } }
            self.send_response(201)
            self.send_header("content-type", "application/json")
            self.end_headers()
            self.wfile.write(bytes(str(res), 'utf-8'))

    def do_GET(self):
        url = urlparse(self.path)
        path = url.path

        if(path.find("/") == 0 ):
            id = path[1:]
            fp = open('data.json', 'r')
            data = json.load(fp)
            ids = [d for d in data if id in d]
            if(len(ids) == 0):
                res = { "error": { "code": 404, "message": "404 Resource Not Found" } }
                self.send_response(404)
                self.send_header("content-type", "application/json")
                self.end_headers()
                self.wfile.write(bytes(str(res), nodeVariables.ENCODING_FORMAT))
            else:
                res={'city' : ids[0][id]}
                self.send_response(201)
                self.send_header("content-type", "application/json")
                self.end_headers()
                self.wfile.write(bytes(str(res), nodeVariables.ENCODING_FORMAT))


if __name__ == "__main__":
    webServer = HTTPServer(
        (nodeVariables.IP_SERVER, nodeVariables.PORT), DBServer)
    print("Server started http://%s:%s" %
          (nodeVariables.IP_SERVER, nodeVariables.PORT))

    try:
        webServer.serve_forever()
    except KeyboardInterrupt:
        pass

    webServer.server_close()
    print("Server stopped.\n")

97dc0f6d8eda4f7d94125deb57cfb8f2d7d4e770
<mask token>


class DBServer(BaseHTTPRequestHandler):

    def do_POST(self):
        url = urlparse(self.path)
        path = url.path
        if path == '/create':
            length = int(self.headers.get('content-length'))
            field_data = self.rfile.read(length)
            entry = json.loads(field_data.decode('utf-8'))
            fp = open('data.json', 'r')
            read = json.load(fp)
            read.append({entry['key']: entry['value']})
            fp.close()
            f = open('data.json', 'w')
            json.dump(read, f)
            f.close()
            res = {'error': {'code': 201, 'message': 'User Added succesfully'}}
            self.send_response(201)
            self.send_header('content-type', 'application/json')
            self.end_headers()
            self.wfile.write(bytes(str(res), 'utf-8'))
    <mask token>


<mask token>

<mask token>


class DBServer(BaseHTTPRequestHandler):

    def do_POST(self):
        url = urlparse(self.path)
        path = url.path
        if path == '/create':
            length = int(self.headers.get('content-length'))
            field_data = self.rfile.read(length)
            entry = json.loads(field_data.decode('utf-8'))
            fp = open('data.json', 'r')
            read = json.load(fp)
            read.append({entry['key']: entry['value']})
            fp.close()
            f = open('data.json', 'w')
            json.dump(read, f)
            f.close()
            res = {'error': {'code': 201, 'message': 'User Added succesfully'}}
            self.send_response(201)
            self.send_header('content-type', 'application/json')
            self.end_headers()
            self.wfile.write(bytes(str(res), 'utf-8'))

    def do_GET(self):
        url = urlparse(self.path)
        path = url.path
        if path.find('/') == 0:
            id = path[1:]
            fp = open('data.json', 'r')
            data = json.load(fp)
            ids = [d for d in data if id in d]
            if len(ids) == 0:
                res = {'error': {'code': 404, 'message':
                    '404 Resource Not Found'}}
                self.send_response(404)
                self.send_header('content-type', 'application/json')
                self.end_headers()
                self.wfile.write(bytes(str(res), nodeVariables.ENCODING_FORMAT)
                    )
            else:
                res = {'city': ids[0][id]}
                self.send_response(201)
                self.send_header('content-type', 'application/json')
                self.end_headers()
                self.wfile.write(bytes(str(res), nodeVariables.ENCODING_FORMAT)
                    )


<mask token>

<mask token>


class DBServer(BaseHTTPRequestHandler):

    def do_POST(self):
        url = urlparse(self.path)
        path = url.path
        if path == '/create':
            length = int(self.headers.get('content-length'))
            field_data = self.rfile.read(length)
            entry = json.loads(field_data.decode('utf-8'))
            fp = open('data.json', 'r')
            read = json.load(fp)
            read.append({entry['key']: entry['value']})
            fp.close()
            f = open('data.json', 'w')
            json.dump(read, f)
            f.close()
            res = {'error': {'code': 201, 'message': 'User Added succesfully'}}
            self.send_response(201)
            self.send_header('content-type', 'application/json')
            self.end_headers()
            self.wfile.write(bytes(str(res), 'utf-8'))

    def do_GET(self):
        url = urlparse(self.path)
        path = url.path
        if path.find('/') == 0:
            id = path[1:]
            fp = open('data.json', 'r')
            data = json.load(fp)
            ids = [d for d in data if id in d]
            if len(ids) == 0:
                res = {'error': {'code': 404, 'message':
                    '404 Resource Not Found'}}
                self.send_response(404)
                self.send_header('content-type', 'application/json')
                self.end_headers()
                self.wfile.write(bytes(str(res), nodeVariables.ENCODING_FORMAT)
                    )
            else:
                res = {'city': ids[0][id]}
                self.send_response(201)
                self.send_header('content-type', 'application/json')
                self.end_headers()
                self.wfile.write(bytes(str(res), nodeVariables.ENCODING_FORMAT)
                    )


if __name__ == '__main__':
    webServer = HTTPServer((nodeVariables.IP_SERVER, nodeVariables.PORT),
        DBServer)
    print('Server started http://%s:%s' % (nodeVariables.IP_SERVER,
        nodeVariables.PORT))
    try:
        webServer.serve_forever()
    except KeyboardInterrupt:
        pass
    webServer.server_close()
    print('Server stopped.\n')

from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
import nodeVariables
import os
import json


class DBServer(BaseHTTPRequestHandler):

    def do_POST(self):
        url = urlparse(self.path)
        path = url.path
        if path == '/create':
            length = int(self.headers.get('content-length'))
            field_data = self.rfile.read(length)
            entry = json.loads(field_data.decode('utf-8'))
            fp = open('data.json', 'r')
            read = json.load(fp)
            read.append({entry['key']: entry['value']})
            fp.close()
            f = open('data.json', 'w')
            json.dump(read, f)
            f.close()
            res = {'error': {'code': 201, 'message': 'User Added succesfully'}}
            self.send_response(201)
            self.send_header('content-type', 'application/json')
            self.end_headers()
            self.wfile.write(bytes(str(res), 'utf-8'))

    def do_GET(self):
        url = urlparse(self.path)
        path = url.path
        if path.find('/') == 0:
            id = path[1:]
            fp = open('data.json', 'r')
            data = json.load(fp)
            ids = [d for d in data if id in d]
            if len(ids) == 0:
                res = {'error': {'code': 404, 'message':
                    '404 Resource Not Found'}}
                self.send_response(404)
                self.send_header('content-type', 'application/json')
                self.end_headers()
                self.wfile.write(bytes(str(res), nodeVariables.ENCODING_FORMAT)
                    )
            else:
                res = {'city': ids[0][id]}
                self.send_response(201)
                self.send_header('content-type', 'application/json')
                self.end_headers()
                self.wfile.write(bytes(str(res), nodeVariables.ENCODING_FORMAT)
                    )


if __name__ == '__main__':
    webServer = HTTPServer((nodeVariables.IP_SERVER, nodeVariables.PORT),
        DBServer)
    print('Server started http://%s:%s' % (nodeVariables.IP_SERVER,
        nodeVariables.PORT))
    try:
        webServer.serve_forever()
    except KeyboardInterrupt:
        pass
    webServer.server_close()
    print('Server stopped.\n')

from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
import nodeVariables
import os
import json
class DBServer(BaseHTTPRequestHandler):
    
    def do_POST(self):
        url = urlparse(self.path)
        path = url.path

        if (path == '/create'):
            length = int(self.headers.get('content-length'))
            field_data = self.rfile.read(length)
            entry = json.loads(field_data.decode('utf-8'))

            fp = open('data.json', 'r')
            read = json.load(fp)
            read.append({entry['key']:entry['value']})
            fp.close()

            f = open('data.json', 'w')
            json.dump(read, f)
            f.close()

            res = { "error": { "code": 201, "message": "User Added succesfully" } }
            self.send_response(201)
            self.send_header("content-type", "application/json")
            self.end_headers()
            self.wfile.write(bytes(str(res), 'utf-8'))

    def do_GET(self):
        url = urlparse(self.path)
        path = url.path

        if(path.find("/") == 0 ):
            id = path[1:]
            fp = open('data.json', 'r')
            data = json.load(fp)
            ids = [d for d in data if id in d]
            if(len(ids) == 0):
                res = { "error": { "code": 404, "message": "404 Resource Not Found" } }
                self.send_response(404)
                self.send_header("content-type", "application/json")
                self.end_headers()
                self.wfile.write(bytes(str(res), nodeVariables.ENCODING_FORMAT))
            else:
                res={'city' : ids[0][id]}
                self.send_response(201)
                self.send_header("content-type", "application/json")
                self.end_headers()
                self.wfile.write(bytes(str(res), nodeVariables.ENCODING_FORMAT))


if __name__ == "__main__":
    webServer = HTTPServer(
        (nodeVariables.IP_SERVER, nodeVariables.PORT), DBServer)
    print("Server started http://%s:%s" %
          (nodeVariables.IP_SERVER, nodeVariables.PORT))

    try:
        webServer.serve_forever()
    except KeyboardInterrupt:
        pass

    webServer.server_close()
    print("Server stopped.\n")
