mirror of
https://github.com/robindhole/fundamentals.git
synced 2025-03-15 21:50:16 +00:00
Adds socket code for multithreaded.
This commit is contained in:
parent
98a8571b5e
commit
52cc49f8d0
17
.vscode/launch.json
vendored
Normal file
17
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Python: Current File",
|
||||||
|
"type": "python",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${file}",
|
||||||
|
"console": "integratedTerminal",
|
||||||
|
"justMyCode": true,
|
||||||
|
"args": ["-H", "127.0.0.1", "-p", "5001"]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
39
networks/code/client.py
Normal file
39
networks/code/client.py
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
from concurrent.futures import thread
|
||||||
|
import socket
|
||||||
|
import argparse
|
||||||
|
from threading import Thread
|
||||||
|
import time
|
||||||
|
|
||||||
|
def main(host: str, port: int, index: int) -> None:
|
||||||
|
|
||||||
|
# Create a socket object
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
|
||||||
|
sock.connect((host, port))
|
||||||
|
print(f"Connected socket {sock.getsockname()} to {sock.getpeername()}")
|
||||||
|
|
||||||
|
data_to_send = bytes(f"Hello, world {index}".encode('utf-8'))
|
||||||
|
print(f"Sending data: {data_to_send}")
|
||||||
|
sock.sendall(data_to_send)
|
||||||
|
|
||||||
|
data = sock.recv(1024)
|
||||||
|
print(f"Received data: {data} \n")
|
||||||
|
|
||||||
|
sock.close()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
# Declare arguments
|
||||||
|
parser = argparse.ArgumentParser(description='Socket server')
|
||||||
|
parser.add_argument('-H', '--host', help='Target host', default='127.0.0.1', type=str)
|
||||||
|
parser.add_argument('-p', '--port', help='Target port', required=True, type=int)
|
||||||
|
|
||||||
|
# Parse arguments
|
||||||
|
args = parser.parse_args()
|
||||||
|
host, port = args.host, args.port
|
||||||
|
|
||||||
|
for index in range(10):
|
||||||
|
thread = Thread(target=main, args=(host, port, index + 1))
|
||||||
|
thread.start()
|
||||||
|
time.sleep(0.2)
|
40
networks/code/port-scanner.py
Normal file
40
networks/code/port-scanner.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import argparse
|
||||||
|
import socket
|
||||||
|
from colorama import init, Fore
|
||||||
|
|
||||||
|
def is_port_open(host: str, port: int) -> bool:
|
||||||
|
"""
|
||||||
|
Checks if a port is open on a host.
|
||||||
|
"""
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
try:
|
||||||
|
sock.connect((host, port))
|
||||||
|
sock.settimeout(0.5)
|
||||||
|
except:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
init() # initialize colorama
|
||||||
|
|
||||||
|
# Declare arguments
|
||||||
|
parser = argparse.ArgumentParser(description='Port Scanner')
|
||||||
|
parser.add_argument('-H', '--host', help='Host to scan', required=True, type=str)
|
||||||
|
parser.add_argument('-s', '--start', help='Starting port', default=1, type=int)
|
||||||
|
parser.add_argument('-e', '--end', help='Ending port', default=1024, type=int)
|
||||||
|
|
||||||
|
# Parse arguments
|
||||||
|
args = parser.parse_args()
|
||||||
|
host, start, end = args.host, args.start, args.end
|
||||||
|
|
||||||
|
for port in range(start, end):
|
||||||
|
if not is_port_open(host, port):
|
||||||
|
print(f'{Fore.RED}Port {port} is closed{Fore.RESET}')
|
||||||
|
continue
|
||||||
|
print(f'{Fore.GREEN}Port {port} is open{Fore.RESET}')
|
||||||
|
|
||||||
|
|
||||||
|
|
50
networks/code/server.py
Normal file
50
networks/code/server.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
import socket
|
||||||
|
import argparse
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
|
def handleConnection(conn) -> None:
|
||||||
|
print(f"Connection from {conn.getsockname()} to {conn.getpeername()}")
|
||||||
|
|
||||||
|
data = conn.recv(1024)
|
||||||
|
print(f"Received data: {data}")
|
||||||
|
|
||||||
|
conn.sendall(data.upper())
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def main(host: str, port:int) -> None:
|
||||||
|
|
||||||
|
# Create a socket object
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
# Bind the socket to the port
|
||||||
|
try:
|
||||||
|
sock.bind((host, port))
|
||||||
|
except socket.error as e :
|
||||||
|
print(f"Error in binding host and port: {e}")
|
||||||
|
|
||||||
|
# Listen for incoming connections
|
||||||
|
sock.listen()
|
||||||
|
print(f"Server is listening on port {port}")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
# Accept connections from outside
|
||||||
|
conn, _ = sock.accept()
|
||||||
|
|
||||||
|
# Delegate the connection to a thread
|
||||||
|
thread = Thread(target=handleConnection, args=(conn,))
|
||||||
|
thread.start()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
# Declare arguments
|
||||||
|
parser = argparse.ArgumentParser(description='Socket server')
|
||||||
|
parser.add_argument('-p', '--port', help='Port to bind to', required=True, type=int)
|
||||||
|
parser.add_argument('-H', '--host', help='Host to bind to', default='127.0.0.1', type=str)
|
||||||
|
|
||||||
|
# Parse arguments
|
||||||
|
args = parser.parse_args()
|
||||||
|
host, port = args.host, args.port
|
||||||
|
|
||||||
|
main(host, port)
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user