add trash server
This commit is contained in:
parent
2934405992
commit
d1b5c65509
6 changed files with 102 additions and 5 deletions
BIN
.DS_Store
vendored
BIN
.DS_Store
vendored
Binary file not shown.
|
|
@ -33,9 +33,9 @@ def bitmap():
|
|||
|
||||
payload = ''.join([chr(i) for i in bitmap_data])
|
||||
print(payload)
|
||||
req = requests.post('http://10.141.142.75/api/tag', data=payload)
|
||||
|
||||
return jsonify({'code': req.status_code})
|
||||
# req = requests.post('http://10.141.142.75/api/tag', data=payload)
|
||||
return jsonify({'payload': payload})
|
||||
# return jsonify({'code': req.status_code})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"error": e}), 500
|
||||
|
|
|
|||
|
|
@ -9,8 +9,13 @@ services:
|
|||
container_name: backend_service
|
||||
ports:
|
||||
- "12001:80"
|
||||
# bitmap_service:
|
||||
# build: ./bitmap_service
|
||||
# container_name: bitmap_service
|
||||
# ports:
|
||||
# - "12002:80"
|
||||
bitmap_service:
|
||||
build: ./bitmap_service
|
||||
container_name: bitmap_service
|
||||
build: ./trash
|
||||
container_name: trash
|
||||
ports:
|
||||
- "12002:80"
|
||||
55
trash/app.py
Normal file
55
trash/app.py
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
from flask import Flask, request, jsonify
|
||||
from flask_cors import CORS
|
||||
from PIL import Image
|
||||
import io
|
||||
from tcp import send_request
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
|
||||
@app.route('/')
|
||||
def root():
|
||||
return 'ok'
|
||||
|
||||
@app.route('/api/bitmap', methods=['POST'])
|
||||
def bitmap():
|
||||
|
||||
if 'file' not in request.files:
|
||||
return jsonify({"error": "no file"}), 400
|
||||
|
||||
file = request.files['file']
|
||||
|
||||
if file.filename == '':
|
||||
return jsonify({"error": "no file"}), 400
|
||||
|
||||
if not file.filename.lower().endswith('.png'):
|
||||
return jsonify({"error": "invalid file format"}), 400
|
||||
|
||||
try:
|
||||
image_stream = io.BytesIO(file.read())
|
||||
img = Image.open(image_stream)
|
||||
grayscale_img = img.convert('L')
|
||||
bitmap_data = list(grayscale_img.getdata())
|
||||
|
||||
payload = ''.join([chr(i) for i in bitmap_data])
|
||||
print(payload)
|
||||
|
||||
chunk_size = 2048
|
||||
for i in range(0, len(payload), chunk_size):
|
||||
data = {
|
||||
"command": "update_image",
|
||||
"index": i,
|
||||
"data": bytes(payload[i:i+chunk_size])
|
||||
}
|
||||
send_request(data)
|
||||
send_request({"command":"push_image"})
|
||||
|
||||
# req = requests.post('http://10.141.142.75/api/tag', data=payload)
|
||||
return jsonify({'payload': payload})
|
||||
# return jsonify({'code': req.status_code})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({"error": e}), 500
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run('0.0.0.0', port=80)
|
||||
9
trash/dockerfile
Normal file
9
trash/dockerfile
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
FROM python:3.9
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN pip3 install pillow flask flask-cors requests
|
||||
|
||||
CMD ["python3", "app.py"]
|
||||
28
trash/tcp.py
Normal file
28
trash/tcp.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
from typing import Dict, Any
|
||||
import socket
|
||||
|
||||
import msgpack
|
||||
|
||||
SERVER_ADDRESS = ('172.16.0.249', 8888)
|
||||
|
||||
def send_request(data:Dict[str, Any]):
|
||||
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
client_socket.connect(SERVER_ADDRESS)
|
||||
|
||||
try:
|
||||
message:bytes = msgpack.packb(data)
|
||||
|
||||
client_socket.sendall(message)
|
||||
|
||||
rx = client_socket.recv(1024)
|
||||
if data:
|
||||
print(f"Received message: {rx.decode('utf-8').strip()}")
|
||||
else:
|
||||
print("Connection closed")
|
||||
return
|
||||
except ConnectionResetError:
|
||||
print("Connection reset")
|
||||
finally:
|
||||
client_socket.close()
|
||||
print("Connection closed")
|
||||
|
||||
Loading…
Add table
Reference in a new issue