-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
62 lines (46 loc) · 1.64 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
mocopiアプリと 1対1 で接続し、
インターネット(Wi-Fi or L5G)経由で表示側PCに送信するスクリプト
1対多は考慮していません
"""
import sys
import time
import socket
import requests
from urllib.parse import urljoin
from concurrent.futures import ThreadPoolExecutor
RECV_PORT = 12350
SEND_PORT = 12351
API_URL = "https://prj-5g-with-mocopi.sysken.net/api/raw/"
SEND_ID = sys.argv[1] if len(sys.argv) > 1 else None
SERVER = sys.argv[2] if len(sys.argv) > 2 else None
if not API_URL:
raise ValueError("API_URL is required. set to env file.")
if not SEND_ID:
raise ValueError("send_id is required. (python main.py <send_id>)")
def send_with_udp(data):
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.sendto(data, (SERVER, SEND_PORT))
s.close()
def send2api(data):
res = requests.post(urljoin(API_URL, SEND_ID), data=data)
now = str(time.time()).encode()
print(f"[{res.status_code}]: {res.text}")
send_data = res + ":".encode() + now
return send_data
def main():
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.bind((SERVER, RECV_PORT))
print(f"[Start] {SERVER}:{RECV_PORT}")
with ThreadPoolExecutor() as executor:
try:
while True:
data, address = s.recvfrom(8192)
print(f"[Received] {address}")
executor.submit(send2api, data)
executor.submit(send_with_udp, data)
except KeyboardInterrupt:
print("Finished!")
executor.shutdown(wait=True)
if __name__ == "__main__":
main()