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