Hello guys..!!

I'm new to python and I have a task which require me to write a simple TCP server which will interact with the web browser. I have done some coding as well as researching over the internet to find relevant notes and materials but none helped.

What I want to do is that I would run a TCP server which will open a socket and listen on some particula port e.g. 12000. Then configure the browser to connect on that port number and try to send a GET request. For example if I type http://localhost:12000/www.google.com the server should be able to return the page and display it on the browser. This is what looks like doing the job but not complete...

# import the required library
import socket, threading

class ClientThread(threading.Thread):

    def __init__(self,ip,port, socket):
        threading.Thread.__init__(self)
        self.ip = ip
        self.port = port
        self.socket = socket
        print "[+] New thread started for "+ip+":"+str(port)

    # Entry point for thread
    def run(self):    
        print "Connection from : "+ip+":"+str(port)

        #clientsock.send("\nWelcome to the server\n\n")
        self.socket.send("\nWelcome to the server\n\n")
        data = "dummydata"

        while len(data):
            data = self.socket.recv(2048)   #clientsock.recv(2048)
            print "Client sent : "+data
            self.socket.sendall("You sent me : " +data)

        print "Client disconnected..."

host = "localhost"
port = 9999

# Create server socket object
tcpsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Port reused immediately after the socket is closed
tcpsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

# Bind the server socket to the port
tcpsock.bind((host,port))
threads = []


while True:
    # Start listening to new connections
    tcpsock.listen(4)
    print "\nListening for incoming connections..."
    (clientsock, (ip, port)) = tcpsock.accept()

    # Create new thread
    newthread = ClientThread(ip, port, clientsock)

    # Start new thread
    newthread.start()

    threads.append(newthread)

# Wait for threads to terminate
for t in threads:
    t.join()

What this code returns is only what the broswer send. I want to know how the server could return the whole html documents (jpge files and anything) so it can be displayed on the broswer. I asking if anyone can point me to the right direction or give some hints that would help.

Thanks.

Are you certain you don't mean to write an HTTP server? TCP is a transfer layer protocol used to send the packets peer-to-peer, and while World Wide Web (HTTP) messages are most often sent across TCP/IP metworks (e.g., the Internet), they are separate things. The server you describe would almost certainly be an HTTP server.

I would look into the http.server library, as that has built-in support for preciesly what you are looking for.

Yes that would be what I'm looking for, HTTP server. I have done awful lot of search online but I could find how the server can return a whole webpage that can be displayed in the browser. I know they are able to interact after sending the GET request but no webpage displayed. Can anyone give me some hint or pseducode. Thanks

I am not sure, I understand your problem.

These two one liners start a webserver in the current directory. If you point your browser to the localhost, and the port given on the commandline, you will get a directory listing.

Python 2.x:
python -m SimpleHTTPServer

Python 3.x:
python -m http.server

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.