X-Git-Url: http://www.dolda2000.com/gitweb/?p=ashd.git;a=blobdiff_plain;f=python%2Fashd%2Futil.py;h=0ff38785094c130fec56ab05d682451c350d8539;hp=08945f2618fb12722a894b889d43723a8624403c;hb=173e0e9efec5ae690cc157fe238113fcd814895e;hpb=188cd02daf85ef68a832deab4fcbf0daaf2d4573 diff --git a/python/ashd/util.py b/python/ashd/util.py index 08945f2..0ff3878 100644 --- a/python/ashd/util.py +++ b/python/ashd/util.py @@ -4,8 +4,8 @@ This module implements a rather convenient interface for writing ashd handlers, wrapping the low-level ashd.proto module. """ -import os, socket, collections -from . import proto +import os, socket +import proto __all__ = ["stdfork", "pchild", "respond", "serveloop"] @@ -27,7 +27,7 @@ def stdfork(argv, chinit = None): if pid == 0: try: os.dup2(csk.fileno(), 0) - for fd in range(3, 1024): + for fd in xrange(3, 1024): try: os.close(fd) except: @@ -131,20 +131,17 @@ def respond(req, body, status = ("200 OK"), ctype = "text/html"): For example: respond(req, "Not found", status = "404 Not Found", ctype = "text/plain") """ - if isinstance(body, collections.ByteString): - body = bytes(body) - else: - body = str(body) - body = body.encode("utf-8") + if type(body) == unicode: + body = body.decode("utf-8") if ctype[:5] == "text/" and ctype.find(';') < 0: ctype = ctype + "; charset=utf-8" + else: + body = str(body) try: - head = "" - head += "HTTP/1.1 %s\n" % status - head += "Content-Type: %s\n" % ctype - head += "Content-Length: %i\n" % len(body) - head += "\n" - req.sk.write(head.encode("ascii")) + req.sk.write("HTTP/1.1 %s\n" % status) + req.sk.write("Content-Type: %s\n" % ctype) + req.sk.write("Content-Length: %i\n" % len(body)) + req.sk.write("\n") req.sk.write(body) finally: req.close()