Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import string,cgi,socket,sys
- from os import curdir,sep,listdir,path,makedirs,pathsep,name
- from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
- ricorda = []
- passw = ""
- loggato = 0
- ip_acc = []
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- s.connect(("google.com",80))
- ip = s.getsockname()
- def ce():
- for i in ip_acc:
- if ip[0] == i:
- return 1
- return 0
- def search_file(filename, search_path):
- file_found = 0
- paths = string.split(search_path, pathsep)
- for pathz in paths:
- if path.exists(path.join(pathz, filename)):
- file_found = 1
- break
- if file_found:
- return path.abspath(path.join(pathz, filename))
- else:
- return 0
- class MyHandler(BaseHTTPRequestHandler):
- def do_GET(self):
- try:
- global ricorda
- self.path = self.path.replace("%20"," ")
- if self.path != "/favicon.ico":
- ricorda.append(self.path)
- if loggato == 0 and ce() == 0 and passw != "":
- self.wfile.write("<html><form method='POST' enctype='multipart/form-data' action=''><b>Stai cercando di accedere ad un computer protetto da password. Inseriscila per continuare:</b>"+'<br><input type="password" name=pw><br><input type=submit value=OK></form></html>')
- else:
- if path.isfile(self.path):
- if self.path == "/hd2web/upload.html":
- self.wfile.write("<HTML><BODY><form method='POST' enctype='multipart/form-data' action=''>File da uploadare: <input type=file name=file><br><br>"+'Nome del file: <input type="text" name=nome><br><br><input type=submit value=Premi> per uploadare il file</form></body></html><br><br>'+'<a href="'+ricorda[len(ricorda)-2]+'">Indietro</a>')
- return
- elif self.path == "/hd2web/about.html":
- self.wfile.write('<html><p align="center"><h1>HD2Web</h1></p>')
- self.wfile.write("<b>Descrizione:</b> rende accessibili tutti i tuoi file da remoto, connettendosi via browser<br>")
- self.wfile.write("<b>Autore:</b> I.Ren<br>")
- self.wfile.write("<b>Licenza:</b> <a href=http://www.gnu.org/copyleft/gpl.html>GNU GPL</a><br>")
- self.wfile.write("<b>Versione:</b> 1.0 ipv4<br>")
- self.wfile.write("<b>Visita:</b> <a href=http://autoit.forumcommunity.net>autoit.forumcommunity.net</a>")
- self.wfile.write("<br><br><font size=2><b>History:</b>")
- self.wfile.write("<br>26/07/2011: rilasciata versione 0.9")
- self.wfile.write("<br>03/08/2011: rilasciata versione 1.0. Aggiornamenti:<ul><li>Bug fixati<li>Aggiunta compatiblita' con windows<li>Aggiunta possibilita' di proteggere con password<li>Aggiuna la possibilita' di cercare cartelleo files</ul>")
- self.wfile.write('</font><br><a href="'+ricorda[len(ricorda)-2]+'">Indietro</a>')
- return
- else:
- self.send_response(200)
- self.send_header('Content-Type', 'application/zip')
- self.send_header('Content-Disposition', 'attachment;'
- 'filename='+self.path[self.path.rfind("/")+1:len(self.path)])
- self.send_header("Content-Transfer-Encoding","binary")
- self.end_headers()
- o = open(self.path)
- s = ""
- for line in o:
- s += line
- o.close()
- self.wfile.write(s)
- return
- else:
- lista = listdir(self.path)
- self.send_response(200)
- self.send_header('Content-type', 'text/html')
- self.end_headers()
- self.wfile.write("<form method='POST' enctype='multipart/form-data action=''><b><a href=/hd2web/upload.html>Upload</a></b> <b><a href=/hd2web/about.html>About</a></b> File/cartella da cercare:"+' <input type="text" name=cercafile> <input type=submit value=cerca></form>')
- if self.path != "/":
- self.wfile.write('<a href="'+self.path[0:self.path.find(self.path.split("/")[len(self.path.split("/"))-2])]+'">..</a><br>')
- for i in sorted(lista):
- if i != "hd2web":
- if path.isfile(self.path+i) == False:
- self.wfile.write('DIR:  <a href="'+self.path+i+'/">'+i+'</a><BR>')
- for i in sorted(lista):
- if path.isfile(self.path+i):
- self.wfile.write("FILE: <a href="+self.path+i+">"+i+"</a><BR>")
- return
- except:
- self.send_error(404,"File not found: "+self.path)
- def do_POST(self):
- global rootnode
- global loggato
- self.path = self.path.replace("%20"," ")
- ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
- if ctype == 'multipart/form-data':
- query = cgi.parse_multipart(self.rfile,pdict)
- self.send_response(200)
- self.end_headers()
- try:
- nome = query.get("nome")
- data = query.get("file")
- dove = ricorda[len( ricorda)-2]
- o = open(dove+nome[0],"w")
- o.write(data[0])
- o.close()
- self.wfile.write('<html><b>Upload effettuato con successo.</b><br><br><a href="'+dove+'">Indietro</a></html>')
- except:
- try:
- g_pass = query.get("pw")
- if g_pass[0] == passw:
- self.wfile.write("<b>Password corretta.</b><br><br><a href=http://"+ip[0]+">Entra</a>")
- loggato = 1
- ip_acc.append(ip[0])
- else:
- self.wfile.write("<b>Password errata.</b><br><br><a href=http://"+ip[0]+">Riprova</a>")
- except:
- nome_fc = query.get("cercafile")
- cerca = search_file(nome_fc[0],self.path)
- self.wfile.write("<html><b>Risultati ricerca</b><br><br>")
- print "---PATH\t" + self.path
- if cerca != 0:
- if name == "nt":
- if path.isfile(cerca[2:len(cerca)]) == False:
- self.wfile.write('<a href="'+cerca[2:len(cerca)]+'/">'+cerca+"</a></html>")
- else:
- self.wfile.write('<a href="'+cerca[2:len(cerca)]+'">'+cerca+"</a></html>")
- else:
- if path.isfile(cerca) == False:
- self.wfile.write('<a href="'+cerca+'/">'+cerca+"</a></html>")
- else:
- self.wfile.write('<a href="'+cerca+'">'+cerca+"</a></html>")
- else:
- self.wfile.write("Nessun file o cartella trovato.")
- if self.path == "/":
- self.wfile.write('<br><br><a href="'+ricorda[len(ricorda)-2]+'">Indietro</a>')
- else:
- self.wfile.write('<br><br><a href="'+ricorda[len(ricorda)-1]+'">Indietro</a>')
- def main():
- global passw
- if not path.exists("/hd2web/"):
- makedirs("/hd2web")
- if not path.exists("/hd2web/upload.html"):
- open("/hd2web/upload.html","w").close()
- if not path.exists("/hd2web/about.html"):
- open("/hd2web/about.html","w").close()
- if not path.exists("/hd2web/cerca.html"):
- open("/hd2web/cerca.html","w").close()
- try:
- if len(sys.argv) > 1:
- porta = sys.argv[1]
- else:
- porta = 80
- if len(sys.argv) == 3:
- porta = sys.argv[1]
- passw = sys.argv[2]
- server = HTTPServer((ip[0],int(porta)),MyHandler)
- print "Server creato su: http://"+ip[0]+":"+str(porta)
- server.serve_forever()
- except KeyboardInterrupt:
- print "^C ricevuto, chiudo il server"
- server.socket.close()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement