Send "http" text via python socket

I am trying to create an HTTP server using python. The fact is that I get everything to work, except to send a response message; if the message has the text http , send() does not work.

Here is the code snippet:

 connectionSocket.send('HTTP/1.1 200 OK text/html') 

Here are the others I've tried:

 connectionSocket.send(''.join('%s 200 OK text/html' % ('HTTP/1.1'))) connectionSocket.send('%s 200 OK text/html' % ('HTTP/1.1')) msg = 'HTTP/1.1 200 OK text/html' for i in range(0, len(msg)) connectionSocket.send(msg[i]) 

The only thing that seems to work is the entity associated with any character in http , e.g.

 connectionSocket.send('HTTP/1.1 200 OK text/html') 

Where H equivalent to H Otherwise, the browser does not display the header received from the python server socket.

The problem also occurs when I try to send a 404 Message on a socket. Other content is displayed, however, as an html file sent over a socket.

I want to know if there is a way to do this? Because if the client is not a browser, the html object will not be understood.

Thanks in advance

Update:

the code:

 from socket import * serverSocket = socket(AF_INET, SOCK_STREAM) serverSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) serverSocket.bind(('127.0.0.1', 1240)) serverSocket.listen(1); while True: print 'Ready to serve...' connectionSocket, addr = serverSocket.accept() try: message = connectionSocket.recv(1024) filename = message.split()[1] f = open(filename[1:]) outputdata = f.read() #Send one HTTP header line into socket connectionSocket.send('HTTP/1.1 200 OK text/html') ## this is not working #Send the content of the requested file to the client for i in range(0, len(outputdata)): connectionSocket.send(outputdata[i]) connectionSocket.close() except IOError: connectionSocket.send('HTTP/1.1 404 File not found') ## this is not working connectionSocket.close(); 

serverSocket.close ()

Screenshots:

Text as "HTTP / 1.1 ..."

enter image description here

enter image description here

Text as "HTTP / 1.1 ..."

enter image description here

enter image description here

HTML code hello.html

 <html> <head> <title>Test Python</title> </head> <body> <h1>Hello World!</h1> </body> </html> 
+7
python browser sockets response
source share
2 answers

You are not returning a well-formed HTTP response. Your line

 connectionSocket.send('HTTP/1.1 200 OK text/html') ## this is not working 

doesn't even end with a new line, and then immediately follows the contents of your file. Protocols such as HTTP pretty accurately indicate what needs to be sent, and I'm a little surprised that you saw anything at all in your browser.

Try something like:

 connectionSocket.send('HTTP/1.1 200 OK\nContent-Type: text/html\n\n') 

This is the start of a well-formed HTTP 1.1 response with a single response line and a single header. A double new line completes the headers, preparing the client to read the next content.

http://www.jmarshall.com/easy/http/ is one of many available ways to learn a little more about the protocol you use. Good luck

+10
source

I'm not sure that you are using connectionSocket (which module, library, etc.), but if this thing is already included in the procedure related to HTTP, it is quite possible that it is already sending the necessary HTTP line without your business. Then yours can disrupt the process.

The cavitated version ( &#72;TTP ...) is probably not recognized by the HTTP protocol in the browser (I think quoting is only recognized and interpreted in the higher layers of the OSI stack) and therefore does not have the same effect.

0
source

All Articles