Search code examples
pythonhtmlsocketshttptcp

unable to retrieve HTML form data running on Localhost:port python


I am running a simple form input on my localhost:port using socket programming.

Currently, I have a form running on my chrome, just a text box on localhost:2333, I am able to see the text box input on my wireshark like this enter image description here

The input message I typed is testesest. After which, I put the <form action="http://localhost:2333"> such that the entered form data can flow back to my localhost:port. However, my 2nd r= recv(1024)is not receiving anything.

import socket
import sys
import os

Addr = ''
PORT = 2333
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((Addr, PORT))
s.listen()

The above is the standard part.

while(1):
    try:
        print("waiting for connection")
        conn, address = s.accept()
        print("New client connected from IP address {} and port number {}".format(*address))
        
        received = conn.recv(1024)
        #print("Request received")

        #This is what i am hosting
        #A webpage with a form
        
        
        conn.send(b'\r\n')
        
        #This is the webpage content

        
        #The code will stuck here at recv
        print("Waiting for form input from client")
        r = conn.recv(1024)
        print(r.decode())

                
        print("Form input received")
        print("HTTP response sent")


    except KeyboardInterrupt:
        conn.close()
        s.close()

    conn.close()
    s.close()
    break   

Can I get some help please?


Solution

  • Input data sent via GET is attached to the URI (/?work=<data>), which is sent as a new request:

    import socket
    import sys
    import os
    
    Addr = ''
    PORT = 2333
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    s.bind((Addr, PORT))
    s.listen()
    
    while (1):
        try:
            print("waiting for connection")
            conn, address = s.accept()
            print(
                "New client connected from IP address {} and port number {}".format(
                    *address
                )
            )
    
            request = conn.recv(1024)
            print("Request received")
            method, uri, _ = request.decode().split(' ', 2)
            print(method, uri)
    
            #This is what i am hosting
            #A webpage with a form
    
            response = ""
            conn.send(b'HTTP/1.1 200 OK\r\n')
            conn.send(b'Content-Type: text/html\r\n')
            conn.send(b'Host: localhost:2333\n')
            conn.send(b'\r\n')
    
            if uri == '/':
                response = """<html>
                    <body><form action="http://localhost:2333/" method="GET">
                    <input type="text" name="work"></form></body>
                    </html>"""
            elif uri.startswith('/?work'):
                response = f"<html><body><h2>recevied: {uri[uri.find('/?work=')+7:]}</h2></body></html>"
    
            conn.send(response.encode())
            conn.send(b"\r\n")
    
            print("Form input received")
            #print("HTTP response sent")
    
        except KeyboardInterrupt:
            conn.close()
            s.close()
    
        #conn.close()
        #s.close()
        #break
    

    Out:

    waiting for connection
    New client connected from IP address 127.0.0.1 and port number 55941
    Request received
    GET /?work=TestInput
    <html><body><h2>recevied: TestInput</h2></body></html>
    Form input received
    waiting for connection
    ...
    

    Note:
    You might want to have a look at the protocol specs and/or use any existing library to get rid of this low level stuff.