File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ import socket
2+
3+ # host and port to connect to
4+ target_host = "0.0.0.0"
5+ target_port = 9998
6+
7+ # create socket object with the socket module
8+ client = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
9+
10+ # connect to the socket
11+ client .connect ((target_host , target_port ))
12+
13+ # send some data
14+ client .send (b"Some data from the client\r \n Host: localhost\r \n \r \n " )
15+
16+ # receive some data
17+ response = client .recv (4096 )
18+
19+ print ("buffer response:\n \n " , response )
20+
21+ print ("\n \n response decoded to utf8:\n \n " , response .decode ("utf8" ))
22+
23+ client .close ()
Original file line number Diff line number Diff line change 1+ import socket
2+ import threading
3+
4+ IP = "0.0.0.0"
5+ PORT = 9998
6+
7+
8+ def main ():
9+ server = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
10+ server .bind ((IP , PORT ))
11+ server .listen (5 )
12+ print (f"[*] Listening on { IP } :{ PORT } " )
13+ # handler loop of the server, it sends each incoming connection
14+ # to a separate thread to be handled
15+ while True :
16+ client , address = server .accept ()
17+ print (f"[*] Accepted connection from { address [0 ]} :{ address [1 ]} " )
18+ client_handler = threading .Thread (target = handle_client , args = (client ,))
19+ client_handler .start ()
20+
21+
22+ def handle_client (client_socket ):
23+ """
24+ function to handle request from the client connected, receives data
25+ from the client and sends example acknowledgement string back to
26+ the client"""
27+ with client_socket as sock :
28+ request = sock .recv (4096 )
29+ print (f'[*] Received: { request .decode ("utf-8" )} ' )
30+ sock .send (b"ACK" )
31+
32+
33+ if __name__ == "__main__" :
34+ main ()
You can’t perform that action at this time.
0 commit comments