I am trying to make a BaseHTTPServer program. I prefer to use Python 3.3 or 3.2 for it. I find the doc hard to understand regarding what to import but tried changing the import from:
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
to:
from http.server import BaseHTTPRequestHandler,HTTPServer
and then the import works and the program start and awaits a GET request. BUT when the request arrives an exception is raised:
File "C:\Python33\lib\socket.py", line 317, in write return self._sock.send(b)
TypeError: 'str' does not support the buffer interface
Question: Is there a version of BaseHTTPServer or http.server that works out of the box with Python3.x or am I doing something wrong?
This is "my" program that I try running in Python 3.3 and 3.2:
#!/usr/bin/python
# from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from http.server import BaseHTTPRequestHandler,HTTPServer
PORT_NUMBER = 8080
# This class will handle any incoming request from
# a browser
class myHandler(BaseHTTPRequestHandler):
# Handler for the GET requests
def do_GET(self):
print ('Get request received')
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
# Send the html message
self.wfile.write("Hello World !")
return
try:
# Create a web server and define the handler to manage the
# incoming request
server = HTTPServer(('', PORT_NUMBER), myHandler)
print ('Started httpserver on port ' , PORT_NUMBER)
# Wait forever for incoming http requests
server.serve_forever()
except KeyboardInterrupt:
print ('^C received, shutting down the web server')
server.socket.close()
The Program work partly in Python2.7 but gives this exception after 2-8 requests:
error: [Errno 10054] An existing connection was forcibly closed by the remote host