projects
/
mw2txt.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
scgi-test: update byte/string handling for Python 2/3 compatability.
[mw2txt.git]
/
posts
/
SCGI
/
scgi-test.py
diff --git
a/posts/SCGI/scgi-test.py
b/posts/SCGI/scgi-test.py
index 4f6ea3d152300e834fe2516980d819afa578d437..36e5b178afd8c714a7d677d14888afb162c04f2a 100755
(executable)
--- a/
posts/SCGI/scgi-test.py
+++ b/
posts/SCGI/scgi-test.py
@@
-20,6
+20,7
@@
"""
import socket as _socket
"""
import socket as _socket
+import sys as _sys
try: # Python 3
import urllib.parse as _urllib_parse
except ImportError: # Python 2
try: # Python 3
import urllib.parse as _urllib_parse
except ImportError: # Python 2
@@
-92,11
+93,14
@@
def recvall(socket):
if not r:
break
ret.append(r)
if not r:
break
ret.append(r)
- return ''.join(ret)
+ return
b
''.join(ret)
def request(socket, data=None, **kwargs):
"""Send a request and return the response string."""
def request(socket, data=None, **kwargs):
"""Send a request and return the response string."""
- socket.sendall(netstring(header(data=data, **kwargs)))
+ ns = netstring(header(data=data, **kwargs))
+ if _sys.version_info >= (3, 0): # Python 3
+ ns = ns.encode('ascii')
+ socket.sendall(ns)
if data:
socket.sendall(data)
return recvall(socket)
if data:
socket.sendall(data)
return recvall(socket)
@@
-125,4
+129,7
@@
if __name__ == '__main__':
finally:
socket.close()
if response:
finally:
socket.close()
if response:
- sys.stdout.write(response)
+ if _sys.version_info >= (3, 0): # Python 3
+ sys.stdout.buffer.write(response)
+ else:
+ sys.stdout.write(response)