-
Notifications
You must be signed in to change notification settings - Fork 0
/
qwerty.py
52 lines (39 loc) · 1.09 KB
/
qwerty.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
#! /usr/bin/python
from socket import socket, AF_UNIX, SOCK_DGRAM
import time
import pathlib
def connect_socket() -> socket:
sock = socket(family=AF_UNIX, type=SOCK_DGRAM)
sock.connect("/tmp/qwerty.socket")
return sock
def bind_socket(sock: socket):
socket_path = pathlib.Path("/tmp/qwerty_anki.socket")
if socket_path.exists():
socket_path.unlink()
sock.bind(str(socket_path))
class Connection:
def __init__(self):
sock = None
sock = connect_socket()
bind_socket(sock)
sock.sendall(b"/start/")
self.sock = sock
def send_word(self, word: str):
self.sock.sendall(word.encode())
def receive_error_times(self) -> int:
buf = self.sock.recv(128)
msg = buf.decode()
return int(msg)
def close(self):
try:
self.sock.sendall(b"/exit/")
self.sock.close()
except OSError:
pass
def main():
con = Connection()
con.send_word("apple")
i = con.receive_error_times()
print("error times:", i)
if __name__ == "__main__":
main()