forked from teja156/simplefileaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftpserver.py
120 lines (88 loc) · 3.27 KB
/
ftpserver.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#from pyftpdlib.authorizers import DummyAuthorizer
#from pyftpdlib.handlers import FTPHandler
#from pyftpdlib.servers import FTPServer
import SimpleHTTPServer
import SocketServer
import BaseHTTPServer
import os
import optparse
import threading
import sys
def checkversion():
ver = sys.version
ver2 = ver[0:1]
vers = int(ver2)
#print(vers)
if(vers>2):
print("This script is not compatible with your version of python, please download python 2.x.x")
exit(1)
checkversion()
USERNAME = "user" #set your username by editing this value within the quotes
PASSWORD = "123456" #set your password by editing the value within the quotes
PATH = "/Users/tejaswaroop/Desktop" #Change this path to the directory on your computer which you want to make accessible by the server
SCRIPT_PATH = "/Users/tejaswaroop/Desktop/simplefileaccess" #Change this path to the location of the downloaded script folder on your computer
ANONYMOUS_PATH = SCRIPT_PATH+"\\anonymous_ftp"
f = open("logs.txt", "a")
uname=""
passwd=""
def initvars():
uname=""
passwd=""
print("PATH : ",PATH)
class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
print("printing request : ",self.path)
st = str(self.path)
f.write(st+"\n")
authorize(st,PATH,ANONYMOUS_PATH) #check if already authorized user, if not, then try to authorize now. If failed, then prohibit access
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def authorize(st,PATH,ANONYMOUS_PATH):
li = list(st)
global uname
global passwd
print("uname is ",uname)
print("passwd is ",passwd)
if(uname==USERNAME and passwd==PASSWORD):
#This means, the user is already authorize so now provide access
ind1 = li.index("/")
add_path = st[ind1:]
new_path = PATH+add_path
print("Providing access to : ",new_path)
else:
#This means the user is not authorized
#Try to authorize the user first
if(("username=" in st) and ("passwd=" in st)):
#This means the request contains the credentials
ind1= li.index("=")
ind2 = li.index("&")
uname = st[ind1+1:ind2]
print("Received username : ",uname)
li = [li[i] for i in range(ind2,len(st))]
ind1 = li.index("=")
for i in range(ind1+1,len(li)):
passwd= passwd+li[i]
print("Received password : ",passwd)
if(uname==USERNAME and passwd==PASSWORD):
os.chdir(PATH)
else:
os.chdir(ANONYMOUS_PATH)
else:
#This means the user is not authorized and not even trying to get authorized, prohibit access
print("Access prohibitted")
os.chdir(SCRIPT_PATH)
def StartServer():
print("Server is up")
host = "127.0.0.1"
port = 1560
handler = SimpleHTTPServer.SimpleHTTPRequestHandler
handler = GetHandler
os.chdir(SCRIPT_PATH)
http_server = SocketServer.TCPServer((host,port),handler)
http_server.serve_forever()
print("Server starting")
initvars()
if __name__ == '__main__':
cthread = threading.Thread(target=StartServer())
cthread.daemon = True
thread.start()
#StartServer()