Sto provando a creare un programma BaseHTTPServer. Preferisco usare Python 3.3 o 3.2 per questo. Trovo il doc difficile da capire per quanto riguarda cosa importare ma provato a cambiare l'importazione da:Python 3.x BaseHTTPServer o http.server
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
a:
from http.server import BaseHTTPRequestHandler,HTTPServer
e poi le opere d'importazione e il programma di avvio e attende una richiesta GET. MA quando la richiesta arriva viene sollevata un'eccezione:
File "C:\Python33\lib\socket.py", line 317, in write return self._sock.send(b)
TypeError: 'str' does not support the buffer interface
Domanda: Esiste una versione di BaseHTTPServer o http.server che funziona out of the box con Python3.x o sto facendo qualcosa di sbagliato?
Questo è il "mio" programma che provo in esecuzione in Python 3.3 e 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()
funzionare il programma in parte in Python2.7 ma dà questa eccezione dopo 2-8 richieste:
error: [Errno 10054] An existing connection was forcibly closed by the remote host
Purtroppo, la norma sui Python 3 documenti non notare cambiamenti da Python 2. Penso che questo è stato un grave errore. –