Skip to content
Snippets Groups Projects
Verified Commit 2829f688 authored by Recolic Keghart's avatar Recolic Keghart
Browse files

init

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #36 failed with stages
in 47 seconds
FROM python:3.7
MAINTAINER root@recolic.net
EXPOSE 8080/tcp
#RUN apt update -y && apt install -y python3-pip && apt clean
RUN pip3 install pickledb
RUN mkdir /app
WORKDIR /app
COPY pickledb_http.py /app/pickledb_http.py
RUN chmod +x pickledb_http.py
CMD /app/pickledb_http.py
# recolic/http-kv
This is a low-performance, slow, unsafe, un-scalable database with only get/set function.
However, it's easy to use and easy to deploy for naive usage. For example, use it in your homework!
#!/usr/bin/env python3
from http.server import HTTPServer, BaseHTTPRequestHandler
from socketserver import ThreadingMixIn
import threading
import pickledb
import sys
http_port = int(sys.argv[1]) if len(sys.argv) > 1 else 8080
db_file = sys.argv[2] if len(sys.argv) > 2 else ''
db = pickledb.load(db_file, False)
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
uri = self.path
message = 'Invalid_Request'
if uri.startswith('/get/'):
kv = uri[5:].split('/')
if len(kv) == 1:
message = db.get(kv[0])
if message == False:
message = ''
elif uri.startswith('/set/'):
kv = uri[5:].split('/')
if len(kv) == 2:
message = str(db.set(kv[0], kv[1]))
self.wfile.write(message.encode('utf-8'))
#self.wfile.write('\n')
return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
if __name__ == '__main__':
server = ThreadedHTTPServer(('0.0.0.0', http_port), Handler)
print('Starting server at 0.0.0.0:{}, use <Ctrl-C> to stop'.format(http_port))
server.serve_forever()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment