-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_server.py
More file actions
38 lines (31 loc) · 1.07 KB
/
local_server.py
File metadata and controls
38 lines (31 loc) · 1.07 KB
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
#!/usr/bin/env python3
import socket
import os, os.path
from time import ctime
BUFSIZ = 1024
TMPFILE = r"/tmp/python_unix_sockets_example"
if os.path.exists(TMPFILE):
os.remove(TMPFILE)
print("Opening socket...")
with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as server:
server.bind(TMPFILE)
server.listen(1)
print("Listening...")
while True:
conn, addr = server.accept()
print("New connection accepted...")
while True:
data = conn.recv(BUFSIZ)
if not data: break
data = data.decode('utf-8')
send_data = "Unknown command"
if data == 'date':
send_data = ctime()
elif data == 'os':
send_data = os.name
elif data == 'ls':
send_data = "\n".join(os.listdir())
elif data.startswith('ls ') and len(data.split()) > 1:
send_data = "\n".join(os.listdir(data.split()[1]))
conn.send(bytes("\n--Response--\n{0}".format(send_data), 'utf-8'))
print("...connection closed")