From 8274849c8a100bc9c6db2192089a2ae3c55fdd7e Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Mon, 25 Jun 2012 19:23:50 +0200 Subject: [PATCH] Moved to new print function and python3 support --- servefile | 93 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/servefile b/servefile index 99f1031..df19f8b 100755 --- a/servefile +++ b/servefile @@ -5,12 +5,13 @@ # Written by Sebastian Lohff (seba@seba-geek.de) # http://seba-geek.de/stuff/servefile/ +from __future__ import print_function + __version__ = '0.4.1' import argparse import base64 import cgi -import BaseHTTPServer import datetime import mimetypes import urllib @@ -18,13 +19,21 @@ import os import posixpath import re import select -import SocketServer import socket from stat import ST_SIZE from subprocess import Popen, PIPE import sys import time +# fix imports for python2/python3 +try: + import BaseHTTPServer + import SocketServer +except ImportError: + # both have different names in python3 + import http.server as BaseHTTPServer + import socketserver as SocketServer + # only activate SSL if available HAVE_SSL = False try: @@ -147,12 +156,12 @@ class FileBaseHandler(BaseHTTPServer.BaseHTTPRequestHandler): while block: try: self.wfile.write(block) - except socket.error, e: - print "%s ABORTED transmission (Reason %s: %s)" % (self.client_address[0], e[0], e[1]) + except socket.error as e: + print("%s ABORTED transmission (Reason %s: %s)" % (self.client_address[0], e[0], e[1])) return False block = self.getChunk(myfile, fromto) myfile.close() - print "%s finished downloading %s" % (self.client_address[0], filePath) + print("%s finished downloading %s" % (self.client_address[0], filePath)) return True def getChunk(self, myfile, fromto): @@ -173,7 +182,7 @@ class FileBaseHandler(BaseHTTPServer.BaseHTTPRequestHandler): responseCode = 200 try: myfile = open(path, 'rb') - except IOError, e: + except IOError as e: responseCode = self.getResponseForErrno(e.errno) return (responseCode, myfile) @@ -188,7 +197,7 @@ class FileBaseHandler(BaseHTTPServer.BaseHTTPRequestHandler): responseCode = 200 try: fileSize = os.stat(path)[ST_SIZE] - except IOError, e: + except IOError as e: responseCode = self.getResponseForErrno(e.errno) return (responseCode, fileSize) @@ -243,7 +252,7 @@ class TarFileHandler(FileBaseHandler): time.sleep(0.05) if tarCmd.poll() != None and tarCmd.poll() != 0: # something went wrong - print "Error while compressing '%s'. Aborting request." % self.target + print("Error while compressing '%s'. Aborting request." % self.target) self.send_response(500) self.end_headers() return @@ -257,7 +266,7 @@ class TarFileHandler(FileBaseHandler): block = tarCmd.stdout.read(self.blockSize) if block and block != '': self.wfile.write(block) - print "%s finished downloading" % (self.client_address[0]) + print("%s finished downloading" % (self.client_address[0])) def getCompressionCmd(self): if self.compression == "none": @@ -490,7 +499,7 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler): bytesLeft -= bytesToRead target.close() self.sendResponse(200, "OK! Thanks for uploading") - print "Received file '%s' from %s." % (destFileName, self.client_address[0]) + print("Received file '%s' from %s." % (destFileName, self.client_address[0])) def do_PUT(self, fromPost=False): """ Upload a file via PUT @@ -584,11 +593,11 @@ def catchSSLErrors(BaseSSLClass): def handle_one_request(self, *args, **kwargs): try: BaseSSLClass.handle_one_request(self, *args, **kwargs) - except SSL.Error, e: + except SSL.Error as e: if str(e) == "": - print "%s SSL Error (Empty error message)" % (self.client_address[0],) + print("%s SSL Error (Empty error message)" % (self.client_address[0],)) else: - print "%s SSL Error: %s" % (self.client_address[0], e) + print("%s SSL Error: %s" % (self.client_address[0], e)) return X @@ -708,7 +717,7 @@ class ServeFile(): TarFileHandler.compression = compression def genKeyPair(self): - print "Generating SSL certificate...", + print("Generating SSL certificate...", end="") sys.stdout.flush() pkey = crypto.PKey() @@ -746,9 +755,9 @@ class ServeFile(): self.cert = cert self.key = pkey - print "done." - print "SHA1 fingerprint:", cert.digest("sha1") - print "MD5 fingerprint:", cert.digest("md5") + print("done.") + print("SHA1 fingerprint:", cert.digest("sha1")) + print("MD5 fingerprint:", cert.digest("md5")) def _getCert(self): return self.cert @@ -798,22 +807,22 @@ class ServeFile(): self.server.append(self._createServer(self.handler, withv6=True)) if self.serveMode != self.MODE_UPLOAD: - print "Serving \"%s\" at port %d." % (self.target, self.port) + print("Serving \"%s\" at port %d." % (self.target, self.port)) else: - print "Serving \"%s\" for uploads at port %d." % (self.target, self.port) + print("Serving \"%s\" for uploads at port %d." % (self.target, self.port)) # print urls with local network adresses - print "\nSome addresses %s will be available at:" % \ - ((self.serveMode != self.MODE_UPLOAD) and "this file" or "the uploadform", ) + print("\nSome addresses %s will be available at:" % \ + ((self.serveMode != self.MODE_UPLOAD) and "this file" or "the uploadform", )) ips = self.getIPs() if not ips or len(ips) == 0 or ips[0] == '': - print "Could not find any addresses." + print("Could not find any addresses.") else: for ip in ips: if ":" in ip: ip = "[%s]" % ip - print "\thttp%s://%s:%d/" % (self.useSSL and "s" or "", ip, self.port) - print "" + print("\thttp%s://%s:%d/" % (self.useSSL and "s" or "", ip, self.port)) + print() try: while True: @@ -851,7 +860,7 @@ class ServeFile(): handler = TarFileHandler elif self.serveMode == self.MODE_UPLOAD: if os.path.isdir(self.target): - print "Warning: Uploading to an already existing directory." + print("Warning: Uploading to an already existing directory.") elif not os.path.exists(self.target): self.dirCreated = True try: @@ -955,54 +964,54 @@ def main(): # check for invalid option combinations/preparse stuff if args.max_upload_size and not args.upload: - print "Error: Maximum upload size can only be specified when in upload mode." + print("Error: Maximum upload size can only be specified when in upload mode.") sys.exit(1) if args.max_upload_size: sizeRe = re.match("^(\d+(?:[,.]\d+)?)(?:([bkmgtpe])(?:(?