-
Notifications
You must be signed in to change notification settings - Fork 0
/
unix_test.py
53 lines (45 loc) · 1.24 KB
/
unix_test.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
import socket
import os
import sys
def bind(sock):
addr = './test.sock'
try:
os.unlink(addr)
except OSError:
if os.path.exists(addr):
raise
sock.bind(addr)
def unix_socket():
return socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
def unixgram_socket():
return socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
def printlen(b):
print(f"recieved {b} bytes from connection")
def main():
args = sys.argv[1:]
if not len(args):
print('what type of unix domain socket do you want.')
if args[0] == 'unix':
sock = unix_socket()
bind(sock)
sock.listen()
print(f"socket listening on {sock.getsockname()}")
while True:
conn, addr = sock.accept()
buf = conn.recv(1024)
printlen(len(buf))
conn.send(buf)
elif args[0] == 'unixgram':
# use unixgram
sock = unixgram_socket()
bind(sock)
print(f"socket listening on {sock.getsockname()}")
while True:
buf, addr = sock.recvfrom(1024)
printlen(len(buf))
print(type(buf), type(addr))
sock.sendto(buf)
else:
print('Fuck YOu!')
if __name__ == '__main__':
main()