Server for both python2 and python3

This commit is contained in:
Thomas Forgione 2020-12-15 15:23:21 +01:00
parent e09c78ebb2
commit d766e1c879
1 changed files with 27 additions and 19 deletions

View File

@ -1,39 +1,45 @@
#! /usr/bin/env python2
#! /usr/bin/env python
# Shamefully copied from this gist :
# https://gist.github.com/pankajp/280596a5dabaeeceaaaa/
# Standard library imports.
from SocketServer import ThreadingMixIn
import BaseHTTPServer
import SimpleHTTPServer
import sys
import json
if sys.version_info[0] < 3:
from SocketServer import ThreadingMixIn
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
from urllib import quote, unquote
from cStringIO import StringIO
else:
import http.server as BaseHTTPServer
from http.server import SimpleHTTPRequestHandler
from socketserver import ThreadingMixIn
from urllib.parse import quote, unquote
from io import BytesIO as cStringIO
import os
from os.path import (join, exists, dirname, abspath, isabs, sep, walk, splitext,
isdir, basename, expanduser, split, splitdrive)
from os.path import (join, exists, abspath, split, splitdrive, isdir)
from os import makedirs, unlink, getcwd, chdir, curdir, pardir, rename, fstat
from shutil import copyfileobj, copytree
import glob
from zipfile import ZipFile
from urlparse import urlparse, parse_qs
from urllib import urlopen, quote, unquote
from posixpath import normpath
from cStringIO import StringIO
import re
import ConfigParser
import cgi
import threading
import socket
import errno
DATA_DIR = getcwd() # join(expanduser('~'), APP_NAME)
DATA_DIR = getcwd()
class ThreadingHTTPServer(ThreadingMixIn, BaseHTTPServer.HTTPServer):
pass
class RequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
class RequestHandler(SimpleHTTPRequestHandler):
""" Handler to handle POST requests for actions.
"""
@ -44,8 +50,7 @@ class RequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
self.range_from, self.range_to = self._get_range_header()
if self.range_from is None:
# nothing to do here
return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
print 'range request', self.range_from, self.range_to
return SimpleHTTPRequestHandler.do_GET(self)
f = self.send_range_head()
if f:
self.copy_file_range(f, self.wfile)
@ -198,11 +203,15 @@ class RequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
""" Returns request Range start and end if specified.
If Range header is not specified returns (None, None)
"""
if sys.version_info[0] < 3:
range_header = self.headers.getheader("Range")
else:
range_header = self.headers.get("Range")
if range_header is None:
return (None, None)
if not range_header.startswith("bytes="):
print "Not implemented: parsing header Range: %s" % range_header
return (None, None)
regex = re.compile(r"^bytes=(\d+)\-(\d+)?")
rangething = regex.search(range_header)
@ -213,7 +222,6 @@ class RequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
else:
return (from_val, None)
else:
print 'CANNOT PARSE RANGE HEADER:', range_header
return (None, None)
@ -245,7 +253,7 @@ def main(args=None):
httpd = get_server(port=PORT, serve_path=serve_path)
print "serving at port", PORT
print("serving at port " + str(PORT))
httpd.serve_forever()
if __name__ == "__main__" :