parent
955f383031
commit
60852d1ad0
@ -0,0 +1,24 @@
|
||||
def unpack(string):
|
||||
ll = string.split('-_')
|
||||
ll.remove("") # Remove empty line
|
||||
el_col = ll[0].count('-')
|
||||
|
||||
post_ll = []
|
||||
for i in ll:
|
||||
temp = i.split("-")
|
||||
# Multi-add (2, 3 or 5 elements)
|
||||
if el_col == 1:
|
||||
post_ll.append([int(temp[0]), int(temp[1])])
|
||||
elif el_col == 2:
|
||||
post_ll.append([int(temp[0]), int(temp[1]), int(temp[2])])
|
||||
elif el_col == 4:
|
||||
post_ll.append([int(temp[0]), int(temp[1]), int(temp[2]), int(temp[3]), int(temp[4]) ])
|
||||
return post_ll
|
||||
|
||||
def pack(ll):
|
||||
string = ''
|
||||
for el in ll:
|
||||
for i in el:
|
||||
string += str(i) + '-'
|
||||
string += '_'
|
||||
return string
|
@ -0,0 +1,19 @@
|
||||
import requests
|
||||
# Work with list-like objects
|
||||
from listwork import *
|
||||
|
||||
server = 'http://127.0.0.1:3333'
|
||||
|
||||
# Draw some pixels
|
||||
# [[x, y, color-1, color-2, color-3], ...]
|
||||
fill = {"main": pack([[200, 100, 0, 0, 0], [15, 30, 255, 54, 43]]) }
|
||||
response = requests.post(server, data=fill)
|
||||
if response:
|
||||
print("posted")
|
||||
|
||||
# Get color code of coords
|
||||
# [[x,y], ...]
|
||||
xys = pack([[200,100], [13,10], [50,50]])
|
||||
response = requests.get(f'{server}/get_color={xys}')
|
||||
print( unpack(response.text) )
|
||||
|
@ -0,0 +1,120 @@
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
from urllib.parse import parse_qs
|
||||
from io import BytesIO
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
import time
|
||||
|
||||
# Easy debug
|
||||
from icecream import ic
|
||||
# ic.disable() # Turn off
|
||||
|
||||
# Limit for requests
|
||||
LTIME = cur_time = time.monotonic()
|
||||
globals().update(locals())
|
||||
|
||||
# Key by value
|
||||
def kbv(dict_, value):
|
||||
for i in dict_:
|
||||
if dict_[i] == value:
|
||||
return i
|
||||
|
||||
# Work with list-like objects
|
||||
from listwork import *
|
||||
|
||||
|
||||
class RequestHandler(BaseHTTPRequestHandler):
|
||||
MATRIX_SIZE = (720, 1024)
|
||||
|
||||
def do_GET(self):
|
||||
params = parse_qs(self.path[1:])
|
||||
ic(params)
|
||||
if 'get_color' in params:
|
||||
xys = unpack(params['get_color'][0])
|
||||
ic(xys)
|
||||
matrix = self.get_matrix()
|
||||
colors = []
|
||||
for i in xys:
|
||||
ic(i)
|
||||
x = i[0] ; y = i[1]
|
||||
color = matrix[x][y]
|
||||
ic(color)
|
||||
|
||||
colors.append(color)
|
||||
|
||||
colors = pack(colors)
|
||||
ic(colors)
|
||||
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
self.end_headers()
|
||||
self.wfile.write(f'{colors}'.encode())
|
||||
|
||||
else:
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'image/png')
|
||||
self.end_headers()
|
||||
matrix = self.get_matrix()
|
||||
matrix = np.flip(matrix, axis=0) #Fix flip on server side
|
||||
self.send_image(matrix)
|
||||
|
||||
def do_POST(self):
|
||||
global LTIME
|
||||
cur_time = time.monotonic()
|
||||
|
||||
if cur_time - LTIME <= 0.0001:
|
||||
self.send_error(429, 'Too Many Requests')
|
||||
self.send_response(429)
|
||||
return 0
|
||||
else:
|
||||
LTIME = time.monotonic()
|
||||
|
||||
content_length = int(self.headers['Content-Length'])
|
||||
body = self.rfile.read(content_length)
|
||||
params = parse_qs(body.decode('utf-8'))["main"]
|
||||
ic(params)
|
||||
|
||||
matrix = self.get_matrix()
|
||||
|
||||
ll = unpack(params[0])
|
||||
for i in ll:
|
||||
x = i[1] ; y = i[0] #Fix (y, x) -> (x, y) on server side
|
||||
matrix[x, y] = [i[2], i[3], i[4]]
|
||||
|
||||
self.save_matrix(matrix)
|
||||
self.send_response(302)
|
||||
self.send_header('Location', '/')
|
||||
self.end_headers()
|
||||
|
||||
def get_matrix(self):
|
||||
try:
|
||||
with open('matrix.npy', 'rb') as f:
|
||||
matrix = np.load(f)
|
||||
except FileNotFoundError:
|
||||
matrix = np.full(shape=(*self.MATRIX_SIZE, 3), fill_value=255, dtype=np.uint8)
|
||||
self.save_matrix(matrix)
|
||||
|
||||
return matrix
|
||||
|
||||
def save_matrix(self, matrix):
|
||||
with open('matrix.npy', 'wb') as f:
|
||||
np.save(f, matrix)
|
||||
|
||||
def send_image(self, matrix):
|
||||
image = Image.fromarray(matrix)
|
||||
buffer = BytesIO()
|
||||
image.save(buffer, format='PNG')
|
||||
self.wfile.write(buffer.getvalue())
|
||||
|
||||
def run():
|
||||
server = HTTPServer(('127.0.0.1', 3333), RequestHandler)
|
||||
server.serve_forever()
|
||||
|
||||
while True:
|
||||
try:
|
||||
run()
|
||||
print(1)
|
||||
except KeyboardInterrupt:
|
||||
exit()
|
||||
except:
|
||||
pass
|
Loading…
Reference in new issue