socket problem and html problem

bussiere bussiere bussiere at gmail.com
Mon Oct 11 02:30:27 EDT 2010


here is my code and two questions :
why it says to me that i can't bind the socket ?
normally it had closed it and kill it :/
and why it returns me plain text and not html ?

Regards and a pack of m&m's to the one who will help me on this two
questions.


import socket
import sys
# Create a TCP/IP socket



class Serverhttp:
    def __init__(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_address = ('localhost', 20000)
        print >>sys.stderr, 'starting up on %s port %s' % server_address
        sock.bind(server_address)
        # 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:
                    if off == 1:
                        sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
                        server_address = ('localhost', 17000)
                        sock.bind(server_address)
                        connection, client_address = sock.accept()
                        off = 0
                    data = connection.recv(1024)
                    print >>sys.stderr, 'received "%s"' % data
                    if data:
                        self.message = self.traitement(data)
                        connection.sendall(self.message)
                        connection.close()
                        sock.close()
                        del(sock)
                        off = 1
                    else:
                        print >>sys.stderr, 'no more data from',
client_address
                        break

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


    def traitement(self,string):
        return
"Content-type:text/html;charset=utf8\n\n<html><body>test</body></html>"
if __name__ == "__main__":
    s = Serverhttp()

Google Fan boy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101011/007caf56/attachment.html>


More information about the Python-list mailing list