views:

55

answers:

2

here is my code :

import socket
import sys
import re
import base64
import binascii
import time
class Serverhttp:
    def __init__(self):
        self.GET = re.compile("GET.*?HTTP")
        self.POST = re.compile("GET.*?HTTP")
        try :
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            server_address = ('localhost', 28000)
            print >>sys.stderr, 'starting up on %s port %s' % server_address
            sock.bind(server_address)
        except :
            time.sleep(2)
            self.__init__()
        # Listen for incoming connections
        sock.listen(1)
        off = 2
        self.message = ""
        while True:
            # Wait for a connection
            print >>sys.stderr, 'waiting for a connection'
            if off == 2 or off == 1:
                connection, client_address = sock.accept()
            try:
                print >>sys.stderr, 'connection from', client_address

                # Receive the data in small chunks and retransmit it
                while True:
                    data = connection.recv(1024)
                    print >>sys.stderr, 'received "%s"' % data
                    if data:
                        self.message = self.traitement(data)
                        connection.sendall(self.message)
                        connection.close()
                        connection, client_address = sock.accept()

                    else:
                        print >>sys.stderr, 'no more data from', client_address
                        break

            finally:
                # Clean up the connection
                connection.close()


    def traitement(self,data):
        url = self.GET.findall(data)
        url = self.POST.findall(data)
        url = url[0].replace("GET","")
        url = url.replace("POST","")
        url = url.replace("HTTP","")
        url = url.replace(" ","")
        print url
        if url == "/favicon.ico":
            return binascii.a2b_base64(
"""AAABAAEAEBAAAAEAIABoBAAAFgAAACgAAAAQAAAAIAAAAAEAIAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAA7PT7/3zF6/9Ptu//RbHx/0227/+Tzvb/9vv5/97h0f9JeBz/NHoA/z98Av9AfAD/
PHsA/0F6AP8AAAAA/vz7/1+33/8Mp+z/FrHw/xWy8f8bs/T/Hqrx/3zE7v////7/t8qp/zF2A/87
gwH/P4ID/z59AP8+egD/Q3kA/97s8v8botj/ELn3/wy58f8PtfL/D7Lw/xuz9P8vq+f/8/n///77
9v9KhR3/OYYA/0GFAv88hgD/QIAC/z17AP/0+/j/N6bM/wC07/8Cxf7/CsP7/wm+9v8Aqur/SrDb
//7+/v///P7/V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AAAAAAAAAAAAAAAA
AAAAAAAAgAEAAA==""")
        else :
            return "Content-type:text/html;charset=utf8\n\n<html><body>test</body></html>"
if __name__ == "__main__":
    s = Serverhttp()

why does the line:

return "Content-type:text/html;charset=utf8\n\n<html><body>test</body></html>"

display plain text in a browser and not html?

+1  A: 

I don't know python but you shouldn't write header informations in HTML. You can specify Content-type with a <meta> in <head> tag.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>test</title>
    </head>
    <body>
        test
    </body>
</html>
MatTheCat
+3  A: 

Replace your return statement with the following

return "HTTP/1.0 200 OK\r\nContent-type:text/html;charset=utf8\r\n\r\n<html><body>test</body></html>"

Note: You should understand how to respond and handle HTTP requests properly. If you are serious in building your own web server, you should first read and understand the HTTP RFC.

Suresh Kumar
regards and thanks for the answers
-1: Correct answer is "Use an existing web server." This answer **should** say "As a fall-back, if you insist on doing twice as much work as necessary, Replace..."
S.Lott
@S Lott: In many cases yes, but what if somebody is trying to learn HTTP? What better way than to build your own?
Suresh Kumar
@Suresh Kumar: The better way is to read the RFC standard, and read an existing web server. Hacking randomly without reading the RFC's leads to questions like this which are (a) already answered in the RFC and (b) already answered by reading the source for an existing server.
S.Lott