A. Test with normal socket application with Class and Def and get the message from FastAPI -> Verify Socket Design ~ Furas
B. Test with normal socket application with Class and Def and accept message for socky and then get the message from FastAPI -> Verifying Socket Design ~ Furas requested thread
C. Test Cannot get data returned0 to client => Actually the myth is only Browsers are allowed to get the data back and not CURL.
D. When the above gives out a negative result do as furas says, thread socket and fastapi on a SPA
E. Create a client socket and bind to server_socket and get the data
F. Create a server socket and browse through browser, bind a new client_socket and recv the data on client and further to UI/UX Methods.
12-10-2025 ~ 19-10-2025
1. Test A passed - Static data returned to FAPI from socket.
2. Test B Failed - Receive data from client and get from socket
3. Test C passed - Works on Browsers
4. Test D Failed - Same as Test B.
5. Test E Passed - Browser can request and Server can log and send the logs to client socket
**Directory Structure:**
$ ls
Cases.txt  Furas_Thread  Wo_Thread 
IUSER+615054627@BT-PF2F406Y /cygdrive/c/Users/615054627/OneDrive - BT Plc/SDET/Functional_BlackBox/Wannabburp/Test Cases
$ sha1sum Furas_Thread/sock_wo_thread.py Wo_Thread/sock_wo_thread.py
1486c3b985c751376b36f45399526bdf606a5794 *Furas_Thread/sock_wo_thread.py
e0822d5eca0e36e9c3f90617d39ed1c1c64bec05 *Wo_Thread/sock_wo_thread.py
**Artifacts:**
**A. Test with normal socket application with Class and Def and get the message from FastAPI**
python .\sock_wo_thread.py
Listening on ('0.0.0.0', 8000)
python -m uvicorn fapi:app --host 0.0.0.0 --port 9000 --reload
$ curl -v http://127.0.0.1:9000/host
*   Trying 127.0.0.1:9000...
* Connected to 127.0.0.1 (127.0.0.1) port 9000
* using HTTP/1.x
> GET /host HTTP/1.1
> Host: 127.0.0.1:9000
> User-Agent: curl/8.13.0
> Accept: */*
>
< HTTP/1.1 200 OK
< date: Sun, 12 Oct 2025 11:24:55 GMT
< server: uvicorn
< content-length: 3
< content-type: application/json
<
"a"* Connection #0 to host 127.0.0.1 left intact
**B.Test with normal socket application with Class and Def and accept message for socky and then get the message from FastAPI**
PS C:\Users\615054627\OneDrive - BT Plc\SDET\Functional_BlackBox\Wannabburp\Test Cases> python .\sock_wo_thread.py
Listening on ('0.0.0.0', 8000)
Connection Accepted <socket.socket fd=1184, family=2, type=1, proto=0, laddr=('127.0.0.1', 8000), raddr=('127.0.0.1', 58585)> ('127.0.0.1', 58585)
The URL requested by the User => GET /host HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: curl/8.13.0
Accept: */*
Boxer Change b'GET /host HTTP/1.1\r\nHost: 127.0.0.1:8000\r\nUser-Agent: curl/8.13.0\r\nAccept: */*\r\n\r\n'
**Client:**
IUSER+615054627@BT-PF2F406Y ~
$ curl -v http://127.0.0.1:9000/host
*   Trying 127.0.0.1:9000...
* Connected to 127.0.0.1 (127.0.0.1) port 9000
* using HTTP/1.x
> GET /host HTTP/1.1
> Host: 127.0.0.1:9000
> User-Agent: curl/8.13.0
> Accept: */*
>
* Request completely sent off
< HTTP/1.1 500 Internal Server Error
< date: Wed, 15 Oct 2025 15:04:04 GMT
< server: uvicorn
< content-length: 21
< content-type: text/plain; charset=utf-8
<
Internal Server Error* Connection #0 to host 127.0.0.1 left intact
**Error:**
  File "C:\Users\615054627\OneDrive - BT Plc\SDET\Functional_BlackBox\Wannabburp\Test Cases\sock_wo_thread.py", line 27, in start_server
    lsock.bind((host, port))
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
PS C:\Users\615054627> netstat -ain | findstr '8000'
TCP    0.0.0.0:8000           0.0.0.0:0              LISTENING          0
TCP    127.0.0.1:8000         127.0.0.1:60377        TIME_WAIT          44306
PS C:\Users\615054627> netstat -ain | findstr '9000'
TCP    192.168.1.6:9000       0.0.0.0:0              LISTENING          0
TCP    192.168.1.6:54009      192.168.1.6:9000       TIME_WAIT          21358
PS C:\Users\615054627>
**C.Test Cannot get data returned0 to client => Actually the myth is only Browsers are allowed to get the data back and not CURL.**
Browser gets the complete Reply and curl is not parsed for this.
The CURL is getting the below Reply.
$ curl -v http://127.0.0.1:8000/host
*   Trying 127.0.0.1:8000...
* Connected to 127.0.0.1 (127.0.0.1) port 8000
* using HTTP/1.x
> GET /host HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/8.13.0
> Accept: */*
>
* abort upload
* Empty reply from server
* shutting down connection #0
curl: (52) Empty reply from server
**D.When the above gives out a negative result do as furas says, thread socket and fastapi on a SPA**
PS C:\Users\615054627\OneDrive - BT Plc\SDET\Functional_BlackBox\Wannabburp\Test Cases\Furas_Thread> python .\sock_wo_thread.py
Listening on ('0.0.0.0', 8000)
INFO:     Started server process [29664]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:9000 (Press CTRL+C to quit)
Connection Accepted <socket.socket fd=1336, family=2, type=1, proto=0, laddr=('127.0.0.1', 8000), raddr=('127.0.0.1', 64954)> ('127.0.0.1', 64954)
The URL requested by the User => GET /host HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: curl/8.13.0
Accept: */*
Boxer Change b'GET /host HTTP/1.1\r\nHost: 127.0.0.1:8000\r\nUser-Agent: curl/8.13.0\r\nAccept: */*\r\n\r\n'
Listening on ('0.0.0.0', 8000)
Client Requests:
==================
**Socket Request:**
IUSER+615054627@BT-PF2F406Y ~
$ curl -v http://127.0.0.1:8000/host
*   Trying 127.0.0.1:8000...
* Connected to 127.0.0.1 (127.0.0.1) port 8000
* using HTTP/1.x
> GET /host HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/8.13.0
> Accept: */*
>
* abort upload
* Empty reply from server
* shutting down connection #0
curl: (52) Empty reply from server
**Fapi Client Request:**
IUSER+615054627@BT-PF2F406Y ~
$ curl -v --max-time 2 http://127.0.0.1:9000/human                              
*   Trying 127.0.0.1:9000...
* Connected to 127.0.0.1 (127.0.0.1) port 9000
* using HTTP/1.x
> GET /human HTTP/1.1
> Host: 127.0.0.1:9000
> User-Agent: curl/8.13.0
> Accept: */*
>
* Request completely sent off
* Operation timed out after 2016 milliseconds with 0 bytes received
* closing connection #0
curl: (28) Operation timed out after 2016 milliseconds with 0 bytes received
**Second Request result to the below Error:**
IUSER+615054627@BT-PF2F406Y ~
$ curl -v --max-time 2 http://127.0.0.1:9000/human
*   Trying 127.0.0.1:9000...
* Connected to 127.0.0.1 (127.0.0.1) port 9000
* using HTTP/1.x
> GET /human HTTP/1.1
> Host: 127.0.0.1:9000
> User-Agent: curl/8.13.0
> Accept: */*
>
< HTTP/1.1 500 Internal Server Error
< date: Fri, 17 Oct 2025 10:18:20 GMT
< server: uvicorn
< content-length: 21
< content-type: text/plain; charset=utf-8
<
* Request completely sent off
Internal Server Error* Connection #0 to host 127.0.0.1 left intact
Error Returned:
  File "C:\Users\615054627\OneDrive - BT Plc\SDET\Functional_BlackBox\Wannabburp\Test Cases\Furas_Thread\sock_wo_thread.py", line 12, in rf
    caller = start_server()
             ^^^^^^^^^^^^^^
  File "C:\Users\615054627\OneDrive - BT Plc\SDET\Functional_BlackBox\Wannabburp\Test Cases\Furas_Thread\sock_wo_thread.py", line 37, in start_server
    lsock.bind((host, port))
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
**E.Create a client socket and bind to server_socket and get the data**
>>> import socket
>>> from sock_wo_thread import *
>>> host = '127.0.0.1'
>>> port = 8000
>>> s = socket.socket()
>>> s.connect((host,port))
>>> s
<socket.socket fd=1192, family=2, type=1, proto=0, laddr=('127.0.0.1', 63595), raddr=('127.0.0.1', 8000)>
>>> s.recvfrom(start_server())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\615054627\OneDrive - BT Plc\SDET\Functional_BlackBox\Wannabburp\Test Cases\Wo_Thread\sock_wo_thread.py", line 27, in start_server
    lsock.bind((host, port))
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
>>>
**F.Create a server socket and browse through browser, bind a new client_socket and recv the data on client and further to Sink or Store for display.**
**Server_Socket_Logs:**
Listening on ('0.0.0.0', 8000)
Connection Accepted <socket.socket fd=832, family=2, type=1, proto=0, laddr=('127.0.0.1', 8000), raddr=('127.0.0.1', 62018)> ('127.0.0.1', 62018)
The URL requested by the User => GET /lk HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: curl/8.13.0
Accept: */*
Boxer Change b'GET /lk HTTP/1.1\r\nHost: 127.0.0.1:8000\r\nUser-Agent: curl/8.13.0\r\nAccept: */*\r\n\r\n'
['GET /lk HTTP/1.1', 'Host: 127.0.0.1:8000', 'User-Agent: curl/8.13.0', 'Accept: */*', '', '']
New socket stream for Clientui
Socket Created with Clientui
['GET /lk HTTP/1.1', 'Host: 127.0.0.1:8000', 'User-Agent: curl/8.13.0', 'Accept: */*', '', '']
Listening on ('0.0.0.0', 8000)
**Client_socket_Logs:**
b'GET /lk HTTP/1.1\r\nHost: 127.0.0.1:8000\r\nUser-Agent: curl/8.13.0\r\nAccept: */*\r\n\r\n'
**Server_Socket_response_to_Browser_with_Headers: Actually Browser Adds some more Headers in request and thus the below repsonse, I am using FireFox**
GET / HTTP/1.1
Host: 127.0.0.1:8000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:144.0) Gecko/20100101 Firefox/144.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br, zstd
DNT: 1
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Priority: u=0, i
Working Code wo using Threading and only browser, sockets - client & server
**Flow -> Browser requests an url, socket_server logs it and forwards the logs to socket_client.**
**socket_server**
import socket
import selectors
import traceback
import requests
HOST = "0.0.0.0"  # Listen on all interfaces
PORT = 8000       # Port to listen on
rHOST = '127.0.0.1'
rPORT = 9000
sel = selectors.DefaultSelector()
#Test Case A wo accept func
class garbage():
    def host():
        hi = 'a'
        return(hi)
    def get():
        return(garbage.host())
        
        
def start_server():
    host, port = HOST, PORT
    lsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    #lsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    lsock.bind((host, port))
    lsock.listen()
    print("\n" f"Listening on {(host, port)}")
    conn, addr = lsock.accept() #Test case B w accept func
    print("Connection Accepted",conn, addr)
    while True:
        with conn:
            try:
                data = (conn.recv(1024)).decode('utf-8')
                print("The URL requested by the User =>", data)
                #raw = ("HTTP/1.1 200 OK") + (data)
                print("Boxer Change", (data.encode('utf-8')))
                #conn.close()
                stor = (data.encode('utf-8')) #Encode as dict for client FF
                sep = (data.split('\r\n')) #Remove trail from data
                print(sep)
                try:
                    s = socket.socket()
                    print("New socket stream for Clientui")
                    s.connect((rHOST,rPORT))
                    print('Socket Created with Clientui')
                    s.sendall(data.encode('utf-8')) 
                    print(sep) #send to socket client
                    s.close()
                except Exception as e:
                    print('Error clientui',e)
                conn.sendall(data.encode('utf-8'))
                #Cannot send the data to browser back
            except Exception as e:
                print('Error conn sendall', e)
            finally:
                return ('i')
    
    
    #lsock.setblocking(False)
    sel.register(lsock, selectors.EVENT_READ, data=None)
    #breakpoint()
    #print(self.received_messages)
    
    try:
        while True:
            events = sel.select(timeout=None)
    except KeyboardInterrupt:
        print("Caught keyboard interrupt, exiting")
    finally:
        sel.close()
def push(start_server):
    return("Boxer Change", (data.encode('utf-8')))
    
   
if __name__ == "__main__":
    # only run socket server if this file is the main program
    try:
        while True:
            start_server()
    except:
        pass
    finally:
        print("Check Error and Adapt")
**client_socket**
import socket
host = '127.0.0.1'
port = 9000
rport = 8000
s = socket.socket()
s.bind((host,port))
s.listen(4096)
while True:
    conn,addr = s.accept()
    msg = conn.recv(1024)
    conn.close()
    print(msg)
The above example is without using threading and FastAPI only browsers and socket_server and socket_client passing messages, Thanks to Furas for the help