2

I am trying to learn some HTTP/CGI stuff and I want to print HTML on the webpage when you view it in your browser but am not sure what the correct syntax is when using the socket library:

#!/usr/bin/env python
import random
import socket
import time

s = socket.socket()         # Create a socket object
host = socket.getfqdn() # Get local machine name
port = 9082
s.bind((host, port))        # Bind to the port

print 'Starting server on', host, port
print 'The Web server URL for this would be http://%s:%d/' % (host, port)

s.listen(5)                 # Now wait for client connection.

print 'Entering infinite loop; hit CTRL-C to exit'
while True:
    # Establish connection with client.    
    c, (client_host, client_port) = s.accept()
    print 'Got connection from', client_host, client_port
    c.send('Server Online\n')
    c.send('HTTP/1.0 200 OK\n')
    c.send('Content-Type: text/html\n')
    c.send(' """\
        <html>
        <body>
        <h1>Hello World</h1> this is my server!
        </body>
        </html>
        """ ')
    c.close()

The first three c.send lines work and then there is a syntax issue with the last line where I put in HTML.

1 Answer 1

4

Use triple-quote string:

c.send("""
    <html>
    <body>
    <h1>Hello World</h1> this is my server!
    </body>
    </html>
""") # Use triple-quote string.

Beside the syntax error, there's multiple issues in the code. Following is a modified version (while loop only, see comments to see what modification made)

while True:
    # Establish connection with client.    
    c, (client_host, client_port) = s.accept()
    print 'Got connection from', client_host, client_port
    #c.send('Server Online\n') # This is invalid HTTP header
    c.recv(1000) # should receive request from client. (GET ....)
    c.send('HTTP/1.0 200 OK\n')
    c.send('Content-Type: text/html\n')
    c.send('\n') # header and body should be separated by additional newline
    c.send("""
        <html>
        <body>
        <h1>Hello World</h1> this is my server!
        </body>
        </html>
    """) # Use triple-quote string.
    c.close()
Sign up to request clarification or add additional context in comments.

5 Comments

It prints the Hello world line, but for some reason doesn't print the c.send('HTTP/1.0 200 OK\n') c.send('Content-Type: text/html\n')
@Goose, They're HTTP headers. They are not printed in the browser.
@Goose, You can check HTTP headers using browser's debugging function. (Firebug for Firebug, Developer Tools in Chrome, ...)
Interesting, the "browser" automatically knows this code to be a header? Must be a standard then?
@Goose, HTTP headers come first, then newline, body after them. See HTTP RFC (warning: It's very long).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.