forcing everything python3, might end up with bugs
This commit is contained in:
104
web/droopy
104
web/droopy
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Droopy (http://stackp.online.fr/droopy)
|
||||
# Copyright 2008-2012 (c) Pierre Duquesne <stackp@online.fr>
|
||||
@@ -52,8 +52,12 @@
|
||||
# * German translation by Michael.
|
||||
# 20080408 * First release.
|
||||
|
||||
import BaseHTTPServer
|
||||
import SocketServer
|
||||
from __future__ import print_function
|
||||
from future import standard_library
|
||||
standard_library.install_aliases()
|
||||
from builtins import str
|
||||
import http.server
|
||||
import socketserver
|
||||
import cgi
|
||||
import os
|
||||
import posixpath
|
||||
@@ -67,10 +71,10 @@ import shutil
|
||||
import tempfile
|
||||
import socket
|
||||
import locale
|
||||
import urllib
|
||||
import urllib.request, urllib.parse, urllib.error
|
||||
|
||||
LOGO = '''\
|
||||
_____
|
||||
_____
|
||||
| \.----.-----.-----.-----.--.--.
|
||||
| -- | _| _ | _ | _ | | |
|
||||
|_____/|__| |_____|_____| __|___ |
|
||||
@@ -88,10 +92,10 @@ Options:
|
||||
--dl provide download links
|
||||
--save-config save options in a configuration file
|
||||
--delete-config delete the configuration file and exit
|
||||
|
||||
|
||||
Example:
|
||||
droopy -m "Hi, this is Bob. You can send me a file." -p avatar.png
|
||||
'''
|
||||
'''
|
||||
|
||||
picture = None
|
||||
message = ""
|
||||
@@ -200,7 +204,7 @@ function update() {
|
||||
}
|
||||
function onunload() {
|
||||
document.getElementById("form").style.display = "block";
|
||||
document.getElementById("sending").style.display = "none";
|
||||
document.getElementById("sending").style.display = "none";
|
||||
}
|
||||
</script></head>
|
||||
<body>
|
||||
@@ -256,7 +260,7 @@ errortmpl = '''
|
||||
''' + userinfo + '''
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
'''
|
||||
|
||||
linkurltmpl = '''<div id="linkurl" class="box">
|
||||
<a href="http://stackp.online.fr/droopy-ip.php?port=%(port)d"> %(discover)s
|
||||
@@ -611,7 +615,7 @@ class DroopyFieldStorage(cgi.FieldStorage):
|
||||
return self.tmpfile
|
||||
|
||||
|
||||
class HTTPUploadHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||
class HTTPUploadHandler(http.server.BaseHTTPRequestHandler):
|
||||
|
||||
protocol_version = 'HTTP/1.0'
|
||||
form_field = 'upfile'
|
||||
@@ -624,9 +628,9 @@ class HTTPUploadHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||
page can be "main", "success", or "error"
|
||||
returns an html page (in the appropriate language) as a string
|
||||
"""
|
||||
|
||||
|
||||
# -- Parse accept-language header
|
||||
if not self.headers.has_key("accept-language"):
|
||||
if "accept-language" not in self.headers:
|
||||
a = []
|
||||
else:
|
||||
a = self.headers["accept-language"]
|
||||
@@ -638,11 +642,11 @@ class HTTPUploadHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||
a.reverse()
|
||||
a = [x[1] for x in a]
|
||||
# now a is an ordered list of preferred languages
|
||||
|
||||
|
||||
# -- Choose the appropriate translation dictionary (default is english)
|
||||
lang = "en"
|
||||
for l in a:
|
||||
if translations.has_key(l):
|
||||
if l in translations:
|
||||
lang = l
|
||||
break
|
||||
dico = copy.copy(translations[lang])
|
||||
@@ -665,7 +669,7 @@ class HTTPUploadHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||
if names:
|
||||
for name in names:
|
||||
links += '<a href="/%s">%s</a><br/>' % (
|
||||
urllib.quote(name.encode('utf-8')),
|
||||
urllib.parse.quote(name.encode('utf-8')),
|
||||
name)
|
||||
links = '<div id="files">' + links + '</div>'
|
||||
dico["files"] = links
|
||||
@@ -682,7 +686,7 @@ class HTTPUploadHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||
|
||||
def do_GET(self):
|
||||
name = self.path.lstrip('/')
|
||||
name = urllib.unquote(name)
|
||||
name = urllib.parse.unquote(name)
|
||||
name = name.decode('utf-8')
|
||||
|
||||
if picture != None and self.path == '/__droopy/picture':
|
||||
@@ -701,7 +705,7 @@ class HTTPUploadHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||
# Do some browsers /really/ use multipart ? maybe Opera ?
|
||||
try:
|
||||
self.log_message("Started file transfer")
|
||||
|
||||
|
||||
# -- Set up environment for cgi.FieldStorage
|
||||
env = {}
|
||||
env['REQUEST_METHOD'] = self.command
|
||||
@@ -714,25 +718,25 @@ class HTTPUploadHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||
form = DroopyFieldStorage(fp = self.rfile, environ = env);
|
||||
fileitem = form[self.form_field]
|
||||
realname=""
|
||||
if self.form_realname in form.keys():
|
||||
if self.form_realname in list(form.keys()):
|
||||
realname = form[self.form_realname].value.decode('utf-8')
|
||||
if realname=="":
|
||||
filename = self.basename(fileitem.filename).decode('utf-8')
|
||||
else:
|
||||
self.log_message("Got realname: %s", realname)
|
||||
filename=self.basename(realname)
|
||||
|
||||
|
||||
if filename == "":
|
||||
self.send_response(303)
|
||||
self.send_header('Location', '/')
|
||||
self.end_headers()
|
||||
return
|
||||
|
||||
|
||||
localpath = os.path.join(directory, filename).encode('utf-8')
|
||||
root, ext = os.path.splitext(localpath)
|
||||
i = 1
|
||||
# race condition, but hey...
|
||||
while (os.path.exists(localpath)):
|
||||
while (os.path.exists(localpath)):
|
||||
localpath = "%s-%d%s" % (root, i, ext)
|
||||
i = i+1
|
||||
if hasattr(fileitem, 'tmpfile'):
|
||||
@@ -757,7 +761,7 @@ class HTTPUploadHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||
else:
|
||||
self.send_html(self.html("success"))
|
||||
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
self.log_message(repr(e))
|
||||
self.send_html(self.html("error"))
|
||||
|
||||
@@ -792,7 +796,7 @@ class HTTPUploadHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||
if publish_files:
|
||||
# os.listdir() returns a list of unicode strings when the
|
||||
# directory is passed as an unicode string itself.
|
||||
names = [name for name in os.listdir(unicode(directory))
|
||||
names = [name for name in os.listdir(str(directory))
|
||||
if os.path.isfile(os.path.join(directory, name))
|
||||
and not name.startswith(DroopyFieldStorage.TMPPREFIX)]
|
||||
names.sort()
|
||||
@@ -802,8 +806,8 @@ class HTTPUploadHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||
|
||||
def handle(self):
|
||||
try:
|
||||
BaseHTTPServer.BaseHTTPRequestHandler.handle(self)
|
||||
except socket.error, e:
|
||||
http.server.BaseHTTPRequestHandler.handle(self)
|
||||
except socket.error as e:
|
||||
self.log_message(str(e))
|
||||
raise Abort()
|
||||
|
||||
@@ -811,14 +815,14 @@ class HTTPUploadHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||
class Abort(Exception): pass
|
||||
|
||||
|
||||
class ThreadedHTTPServer(SocketServer.ThreadingMixIn,
|
||||
BaseHTTPServer.HTTPServer):
|
||||
class ThreadedHTTPServer(socketserver.ThreadingMixIn,
|
||||
http.server.HTTPServer):
|
||||
|
||||
def handle_error(self, request, client_address):
|
||||
# Override SocketServer.handle_error
|
||||
exctype = sys.exc_info()[0]
|
||||
if not exctype is Abort:
|
||||
BaseHTTPServer.HTTPServer.handle_error(self,request,client_address)
|
||||
http.server.HTTPServer.handle_error(self,request,client_address)
|
||||
|
||||
|
||||
# -- Options
|
||||
@@ -858,7 +862,7 @@ def save_options():
|
||||
f.write('\n'.join(opt).encode('utf8'))
|
||||
f.close()
|
||||
|
||||
|
||||
|
||||
def load_options():
|
||||
try:
|
||||
f = open(configfile())
|
||||
@@ -867,7 +871,7 @@ def load_options():
|
||||
parse_args(cmd)
|
||||
f.close()
|
||||
return True
|
||||
except IOError, e:
|
||||
except IOError as e:
|
||||
return False
|
||||
|
||||
|
||||
@@ -883,7 +887,7 @@ def parse_args(cmd=None):
|
||||
lang, encoding = locale.getdefaultlocale()
|
||||
if encoding != None:
|
||||
cmd = [a.decode(encoding) for a in cmd]
|
||||
|
||||
|
||||
opts, args = None, None
|
||||
try:
|
||||
opts, args = getopt.gnu_getopt(cmd, "p:m:d:h",
|
||||
@@ -891,8 +895,8 @@ def parse_args(cmd=None):
|
||||
"directory=", "help",
|
||||
"save-config","delete-config",
|
||||
"dl"])
|
||||
except Exception, e:
|
||||
print e
|
||||
except Exception as e:
|
||||
print(e)
|
||||
sys.exit(1)
|
||||
|
||||
for o,a in opts:
|
||||
@@ -904,7 +908,7 @@ def parse_args(cmd=None):
|
||||
|
||||
elif o in ['-d', '--directory']:
|
||||
directory = a
|
||||
|
||||
|
||||
elif o in ['--save-config']:
|
||||
must_save_options = True
|
||||
|
||||
@@ -912,16 +916,16 @@ def parse_args(cmd=None):
|
||||
try:
|
||||
filename = configfile()
|
||||
os.remove(filename)
|
||||
print 'Deleted ' + filename
|
||||
except Exception, e:
|
||||
print e
|
||||
print('Deleted ' + filename)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
sys.exit(0)
|
||||
|
||||
elif o in ['--dl']:
|
||||
publish_files = True
|
||||
|
||||
elif o in ['-h', '--help']:
|
||||
print USAGE
|
||||
print(USAGE)
|
||||
sys.exit(0)
|
||||
|
||||
# port number
|
||||
@@ -929,11 +933,11 @@ def parse_args(cmd=None):
|
||||
if args[0:]:
|
||||
port = int(args[0])
|
||||
except ValueError:
|
||||
print args[0], "is not a valid port number"
|
||||
print(args[0], "is not a valid port number")
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
# --
|
||||
# --
|
||||
|
||||
def run():
|
||||
"""Run the webserver."""
|
||||
@@ -944,25 +948,25 @@ def run():
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print LOGO
|
||||
print(LOGO)
|
||||
|
||||
config_found = load_options()
|
||||
parse_args()
|
||||
|
||||
if config_found:
|
||||
print 'Configuration found in %s' % configfile()
|
||||
print('Configuration found in %s' % configfile())
|
||||
else:
|
||||
print "No configuration file found."
|
||||
|
||||
print("No configuration file found.")
|
||||
|
||||
if must_save_options:
|
||||
save_options()
|
||||
print "Options saved in %s" % configfile()
|
||||
print("Options saved in %s" % configfile())
|
||||
|
||||
print "Files will be uploaded to %s" % directory
|
||||
print("Files will be uploaded to %s" % directory)
|
||||
try:
|
||||
print
|
||||
print "HTTP server running... Check it out at http://localhost:%d"%port
|
||||
print()
|
||||
print("HTTP server running... Check it out at http://localhost:%d"%port)
|
||||
run()
|
||||
except KeyboardInterrupt:
|
||||
print '^C received, shutting down server'
|
||||
print('^C received, shutting down server')
|
||||
# some threads may run until they terminate
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#!/bin/sh
|
||||
|
||||
python -c "import pyftpdlib" >/dev/null 2>&1 || {
|
||||
python3 -c "import pyftpdlib" >/dev/null 2>&1 || {
|
||||
echo "pyftpdlib is not installed:"
|
||||
echo " pip install --user pyftpdlib"
|
||||
exit
|
||||
echo " pip3 install --user pyftpdlib"
|
||||
exit
|
||||
}
|
||||
python -m pyftpdlib "$@"
|
||||
python3 -m pyftpdlib "$@"
|
||||
|
||||
Reference in New Issue
Block a user