Commit 04c0d5f7 authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

recipe/simplehttpserver: add support for Python 3

parent 67d76c7e
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
from slapos.recipe.librecipe import GenericBaseRecipe from slapos.recipe.librecipe import GenericBaseRecipe
import string, random import string, random
import os import os
from six.moves import range
class Recipe(GenericBaseRecipe): class Recipe(GenericBaseRecipe):
...@@ -35,7 +36,7 @@ class Recipe(GenericBaseRecipe): ...@@ -35,7 +36,7 @@ class Recipe(GenericBaseRecipe):
base_path = options['base-path'] base_path = options['base-path']
if options.get('use-hash-url', 'True') in ['true', 'True']: if options.get('use-hash-url', 'True') in ['true', 'True']:
pool = string.letters + string.digits pool = string.letters + string.digits
hash_string = ''.join(random.choice(pool) for i in xrange(64)) hash_string = ''.join(random.choice(pool) for i in range(64))
path = os.path.join(base_path, hash_string) path = os.path.join(base_path, hash_string)
if os.path.exists(base_path): if os.path.exists(base_path):
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from SimpleHTTPServer import SimpleHTTPRequestHandler from six.moves.SimpleHTTPServer import SimpleHTTPRequestHandler
from BaseHTTPServer import HTTPServer from six.moves.BaseHTTPServer import HTTPServer
import ssl import ssl
import os import os
import logging import logging
...@@ -8,6 +8,9 @@ from netaddr import valid_ipv4, valid_ipv6 ...@@ -8,6 +8,9 @@ from netaddr import valid_ipv4, valid_ipv6
import socket import socket
import cgi, errno import cgi, errno
from slapos.util import str2bytes
class ServerHandler(SimpleHTTPRequestHandler): class ServerHandler(SimpleHTTPRequestHandler):
document_path = '' document_path = ''
...@@ -22,7 +25,7 @@ class ServerHandler(SimpleHTTPRequestHandler): ...@@ -22,7 +25,7 @@ class ServerHandler(SimpleHTTPRequestHandler):
if self.restrict_root_folder and self.path and self.path == '/': if self.restrict_root_folder and self.path and self.path == '/':
# no access to root path # no access to root path
self.respond(403) self.respond(403)
self.wfile.write("Forbidden") self.wfile.write(b"Forbidden")
return True return True
return False return False
...@@ -46,11 +49,11 @@ class ServerHandler(SimpleHTTPRequestHandler): ...@@ -46,11 +49,11 @@ class ServerHandler(SimpleHTTPRequestHandler):
name = form['path'].value name = form['path'].value
content = form['content'].value content = form['content'].value
method = 'a' method = 'a'
if form.has_key('clear') and form['clear'].value == '1': if 'clear' in form and form['clear'].value == '1':
method = 'w' method = 'w'
self.writeFile(name, content, method) self.writeFile(name, content, method)
self.respond(200, type=self.headers['Content-Type']) self.respond(200, type=self.headers['Content-Type'])
self.wfile.write("Content written to %s" % name) self.wfile.write(b"Content written to %s" % str2bytes(name))
def writeFile(self, filename, content, method='a'): def writeFile(self, filename, content, method='a'):
file_path = os.path.join(self.document_path, filename) file_path = os.path.join(self.document_path, filename)
...@@ -97,7 +100,7 @@ def run(args): ...@@ -97,7 +100,7 @@ def run(args):
httpd = server((host, port), Handler) httpd = server((host, port), Handler)
scheme = 'http' scheme = 'http'
if args.has_key('cert-file') and args.has_key('key-file') and \ if 'cert-file' in args and 'key-file' in args and \
os.path.exists(args['cert-file']) and os.path.exists(args['key-file']): os.path.exists(args['cert-file']) and os.path.exists(args['key-file']):
scheme = 'https' scheme = 'https'
httpd.socket = ssl.wrap_socket (httpd.socket, httpd.socket = ssl.wrap_socket (httpd.socket,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment