Sending http headers with python
Asked Answered
V

3

8

I've set up a little script that should feed a client with html.

import socket

sock = socket.socket()
sock.bind(('', 8080))
sock.listen(5)
client, adress = sock.accept()


print "Incoming:", adress
print client.recv(1024)
print

client.send("Content-Type: text/html\n\n")
client.send('<html><body></body></html>')

print "Answering ..."
print "Finished."

import os
os.system("pause")

But it is shown as plain text in the browser. Can you please tell what I need to do ? I just can't find something in google that helps me..

Thanks.

Viewy answered 29/11, 2011 at 17:40 Comment(0)
T
18

The response header should include a response code indicating success. Before the Content-Type line, add:

client.send('HTTP/1.0 200 OK\r\n')

Also, to make the test more visible, put some content in the page:

client.send('<html><body><h1>Hello World</body></html>')

After the response is sent, close the connection with:

client.close()

and

sock.close()

As the other posters have noted, terminate each line with \r\n instead of \n.

Will those additions, I was able to run successful test. In the browser, I entered localhost:8080.

Here's all the code:

import socket

sock = socket.socket()
sock.bind(('', 8080))
sock.listen(5)
client, adress = sock.accept()

print "Incoming:", adress
print client.recv(1024)
print

client.send('HTTP/1.0 200 OK\r\n')
client.send("Content-Type: text/html\r\n\r\n")
client.send('<html><body><h1>Hello World</body></html>')
client.close()

print "Answering ..."
print "Finished."

sock.close()
Tobacco answered 29/11, 2011 at 17:47 Comment(6)
... and don't forget to replace \n\n to \r\n\r\n, since HTTP needs CRLF to be send after headers.Dehiscence
and better to use '\r\n\r\n' instead of '\n\n'Panhellenic
Wow, thats it. Thank you ! Are there any related documents about the exchange between server and client with the http protocol ?Viewy
@NiklasR You can start with an overview on wikipedia en.wikipedia.org/wiki/Hypertext_Transfer_Protocol and then get the details at w3.org/Protocols/rfc2616/rfc2616.htmlTobacco
@RaymondHettinger,I was wondering how the browser could recognize the end of http response body without Content-Length in the http response headerLeviathan
@GaryGauh Content-Length is necessary if you leave the connection open. But if you close the connection as we do above, the browser can interpret "hanging-up the phone" as the "there is no more content coming".Tobacco
T
0

webob does the dirty http details for you as well

from webob import Response
....

client.send(str(Response("<html><body></body></html>")))
Th answered 29/11, 2011 at 20:22 Comment(1)
Note that webob uses \n to separate lines instead of the correct \r\n. This is #146. Browsers don't seem to care, though, so your solution will work fine in most cases.Krisha
A
0

Python 3 variant of @Raymond's snippet (suitable for viewing browser's http request headers).

import socket

sock = socket.socket()
sock.bind(('', 8080)) # ('127.0.0.1', 8080) for localhost
sock.listen(5)
client, address = sock.accept()

print('Incoming:', address)
print(client.recv(1024))
print()

encoding = 'ascii'
client.send(bytes('HTTP/1.0 200 OK\r\n', encoding))
client.send(bytes('Content-Type: text/html\r\n\r\n', encoding))
client.send(bytes('<html><body><h1>Hello World</h1></body></html>', encoding))
client.close()

print('Answering ...')
print('Finished.')

sock.close()
Apeman answered 22/8, 2023 at 14:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.