Dolda2000 GitWeb
/
ashd.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
python: Initial porting of the Python code to Python 3.
[ashd.git]
/
python
/
ashd
/
util.py
diff --git
a/python/ashd/util.py
b/python/ashd/util.py
index
2ac7dc0
..
08945f2
100644
(file)
--- a/
python/ashd/util.py
+++ b/
python/ashd/util.py
@@
-4,8
+4,10
@@
This module implements a rather convenient interface for writing ashd
handlers, wrapping the low-level ashd.proto module.
"""
handlers, wrapping the low-level ashd.proto module.
"""
-import os, socket
-import proto
+import os, socket, collections
+from . import proto
+
+__all__ = ["stdfork", "pchild", "respond", "serveloop"]
def stdfork(argv, chinit = None):
"""Fork a persistent handler process using the `argv' argument
def stdfork(argv, chinit = None):
"""Fork a persistent handler process using the `argv' argument
@@
-25,7
+27,7
@@
def stdfork(argv, chinit = None):
if pid == 0:
try:
os.dup2(csk.fileno(), 0)
if pid == 0:
try:
os.dup2(csk.fileno(), 0)
- for fd in
x
range(3, 1024):
+ for fd in range(3, 1024):
try:
os.close(fd)
except:
try:
os.close(fd)
except:
@@
-129,17
+131,20
@@
def respond(req, body, status = ("200 OK"), ctype = "text/html"):
For example:
respond(req, "Not found", status = "404 Not Found", ctype = "text/plain")
"""
For example:
respond(req, "Not found", status = "404 Not Found", ctype = "text/plain")
"""
- if type(body) == unicode:
- body = body.decode("utf-8")
- if ctype[:5] == "text/" and ctype.find(';') < 0:
- ctype = ctype + "; charset=utf-8"
+ if isinstance(body, collections.ByteString):
+ body = bytes(body)
else:
body = str(body)
else:
body = str(body)
+ body = body.encode("utf-8")
+ if ctype[:5] == "text/" and ctype.find(';') < 0:
+ ctype = ctype + "; charset=utf-8"
try:
try:
- 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")
+ 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(body)
finally:
req.close()
req.sk.write(body)
finally:
req.close()