-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
65 lines (54 loc) · 1.83 KB
/
server.py
File metadata and controls
65 lines (54 loc) · 1.83 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
import sys
from socket import *
import subprocess
def bind_(host, port):
st = socket(AF_INET, SOCK_STREAM)
st.bind((host, port))
st.listen(5)
while 1:
con, addr = st.accept()
while 1:
try:
cmd = con.recv(30)
# print cmd
if "lst" in cmd:
pop = subprocess.Popen(['ls', '-1'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
stdin=subprocess.PIPE)
data = pop.stdout.read()
con.send(data)
elif "get" in cmd:
foo = cmd.split("get ")[1].split("\r")[0]
try:
off = open(foo, 'r')
data = off.read()
con.send("ok\r\n")
con.send(str(off.tell()) + "\r\n")
con.send(data + "\r\n")
except:
con.send("wr\r\n")
elif "put" in cmd:
print "i am inside put()"
foo = cmd.split("put ")[1].split("\r")[0]
print "yeah i got your file name "
# print repr(foo)
o = open(foo, 'wb')
l = con.recv(1024).strip()
con.settimeout(5)
while (1):
o.write(l)
try:
l = con.recv(1024).strip()
except:
break
o.close()
else:
continue
except:
continue
con.close()
st.close()
if __name__ == "__main__":
server = "127.0.0.1"
port = int(sys.argv[1])
bind_(server, port)