Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Session 03 - Python for Forensics

The code here is to support the Session 03 lecture.

tcpServer.py

import socket, sys

def server():
    host = "127.0.0.1"
    port = 5000

    try:
        serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        print("[+] Socket successfully created!")

        serverSocket.bind((host, port))
        print("[+] Socket bound to " + host + " on " + str(port) + "!\n")

        serverSocket.listen(2)
        print("[+] Successfully created bi-directional sockets...")
        print("[i] Server accepting incoming connection now...")

        conn, address = serverSocket.accept()
        print("[i] Detected incoming connection from " + str(address))

    except socket.error as error:
        print("[E] There was an issue with the socket, due to %s" %(error))
        print("[!] Exiting...")
        sys.exit()

    while True:
        data = conn.recv(1024).decode()
        if not data:
            break
        print(" (received message): " + str(data))
        data = input(" (send message): ")
        conn.send(data.encode())

    conn.close()

if __name__ == '__main__':
    server()
[ Last updated: Tuesday 13 February 2024 @ 11:22:49 +0000 // branch: master // commit: bd41815 ]