Python 3.x BaseHTTPServer or http.server -
I am trying to create a BaseHttps server program. I prefer to use Python 3.3 or 3.2 for this. To understand the importation, I get a hard dock but try to change the import from: HTTP For
: Import and then the import works and the program starts and GET requests But a request exception is raised when: Question: Is there a version of the BaseHttpSaer or http sir Works with 3.x or am I doing something wrong? This is my "" program that I try to run in Python 3.3 and 3.2: The program works partially in Python2.7, but after 2-8 requests it gives an exception: in dragon 3.xx Your program Works just outside the box - except a small problem. This issue is not in your code, but the place where you are writing these lines: You are trying to write "string" there, but the bytes should go there. So you have to convert your string into bytes. Here, see my code, which is similar to yours and works perfectly, it has been written in Python 3.4, http.server importBaseHTTPRequestHandler to HTTPServer import time hostname = "localhost" hostPort = 9000 Class MySaer (baseHTPPRequesthandler): def do_GET (self): self.send_response (200)) self.send_header ("content type", "text / html") self.end_headers () self.wfile.write (bytes ("& lt ; html> gt; & gt; Title & gt; Title goes here. & lt; / titles & gt; & lt; / head & gt; "," utf-8 ")) self .wfile.write (bytes ("& lt; body> Please note that the way they have been converted from string to byte please note es "UTF-8 "Using this encoding to After tan, your program should work fine. BaseHTTPRequestHandler, HTTPServer
The file "C: \ Python33 \ lib \ socket.py", line 317, l Self._sock.send (b) TypeError: 'Str' does not support the buffer interface
#! Import from / usr / bin / python # to BaseHttpServer from BaseHTTPRequestHandler, HTTPServer http. Server Import BaseHTTPRequestHandler, HTTPServer PORT_NUMBER = 8080 # This class # handles any incoming request from a browser class myHandler (BaseHTTPRequestHandler): # Handler Def DO_GET (self) for GET requests: Print (Get 'Receive Request') self.send_response Try to return self.send_header ('content-type', 'text / html') self.end_headers () # Send HTML messages self.wfile.write ("Hello World!"): #Web server Create and define headlar # i to request incoming requests sir (Waiting for incoming HTTP requests, server interface web server ') server.socket.close (); = HTTPServer (' ', PORT_NUMBER), myHandler) Print (' httpserverver started on port ', PORT_NUMBER)
Error: [Errron 10054 ] An existing connection was forced by the remote host
self.wfile.write ("Hello World!")
Comments
Post a Comment