Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 support #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
6 changes: 3 additions & 3 deletions part1/webserver1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(1)
print 'Serving HTTP on port %s ...' % PORT
print('Serving HTTP on port %s ...' % PORT)
while True:
client_connection, client_address = listen_socket.accept()
request = client_connection.recv(1024)
print request
print(request)

http_response = """\
HTTP/1.1 200 OK

Hello, World!
"""
client_connection.sendall(http_response)
client_connection.sendall(http_response.encode('utf-8'))
client_connection.close()
16 changes: 9 additions & 7 deletions part2/webserver2.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Tested with Python 2.7.9, Linux & Mac OS X
try:
from StringIO import StringIO # Python 2
except:
from io import StringIO # Python 3
import socket
import StringIO
import sys


class WSGIServer(object):

address_family = socket.AF_INET
Expand Down Expand Up @@ -62,7 +63,7 @@ def handle_one_request(self):
self.finish_response(result)

def parse_request(self, text):
request_line = text.splitlines()[0]
request_line = text.splitlines()[0].decode('utf-8')
request_line = request_line.rstrip('\r\n')
# Break down the request line into components
(self.request_method, # GET
Expand All @@ -79,7 +80,7 @@ def get_environ(self):
# Required WSGI variables
env['wsgi.version'] = (1, 0)
env['wsgi.url_scheme'] = 'http'
env['wsgi.input'] = StringIO.StringIO(self.request_data)
env['wsgi.input'] = StringIO(self.request_data.decode('utf-8'))
env['wsgi.errors'] = sys.stderr
env['wsgi.multithread'] = False
env['wsgi.multiprocess'] = False
Expand Down Expand Up @@ -111,13 +112,14 @@ def finish_response(self, result):
response += '{0}: {1}\r\n'.format(*header)
response += '\r\n'
for data in result:
response += data
response += data.decode('utf-8')
# Print formatted response data a la 'curl -v'
print(''.join(
'> {line}\n'.format(line=line)
for line in response.splitlines()
))
self.client_connection.sendall(response)
#self.client_connection.sendall(bytes(response, 'UTF-8'))
self.client_connection.sendall(response.encode('utf-8'))
finally:
self.client_connection.close()

Expand Down
2 changes: 1 addition & 1 deletion part2/wsgiapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ def app(environ, start_response):
status = '200 OK'
response_headers = [('Content-Type', 'text/plain')]
start_response(status, response_headers)
return ['Hello world from a simple WSGI application!\n']
return ['Hello world from a simple WSGI application!\n'.encode('utf-8')]