Class 6 Lab 1 ‐ Server Socket - Justin-Boyd/Python-Class GitHub Wiki

Task 1: Server Socket Creation

Step 1

  • Open PyCharm and create a new project by clicking File and selecting New Project.

Step 2

  • Name the project, select Inherit global site-packages and Make available to all projects, and click Create.

Step 3

  • Create a new Python file by right clicking the project’s folder and selecting New > Python File. Name the file Server.

Step 4

  • Import the module socket to the file.
import socket

Step 5

  • Create a socket variable.
my_sock = socket.socket()

Step 6

  • Bind the socket to accept connection from all IP addresses on port 4444.
my_sock.bind(("0.0.0.0", 4444))

Step 7

  • Allow only one connection to the socket.
my_sock.listen(1)

Step 8

  • Allow a connection to the socket and save the connection object and the address to variables.
connection, address = my_sock.accept()

Step 9

  • Close the connection.
my_sock.close()

Step 10

  • Right click the file and run the code.

Task 2: Server Socket Creation

Step 1

  • Open VirtualBox, click the Machine tab, click New, and name the VM Kali Linux.

Step 2

  • Set the memory to 2048 MB for proper functionality. You can use a larger setting (in accordance with the computer’s available resources) for enhanced functionality.

Step 3

  • In the next window, select Create a virtual hard disk now and click Create.

Step 4

  • Select VHD for the hard disk file type and click Next.

Step 5

  • Select Dynamically allocated and click Next.

Step 6

  • Select the file location and set the storage size to 50 GB. If your computer does not have enough storage, you can set it to 20 GB.

Step 7

  • Right-click the Kali VM, go to Settings, and insert the Kali-linux-2019.3- amd64.iso in the drive.

Step 8

  • Click Network and make sure the machine is set to Bridged Adapter.

Step 9

  • Run the virtual machine and select the live version.

Step 10

  • Open the terminal by clicking its icon in the option bar on left side of the window.

Step 11

  • Run the command netcat -v .

Final Code

"""Lab Objective: Implement the required commands to create a listening server."""
import socket

my_sock = socket.socket()
my_sock.bind(("0.0.0.0", 4444))
my_sock.listen(1)
connection, address = my_sock.accept()
my_sock.close()

# prevents program from closing upon execution
input("\nPress 'Enter' to exit the program")
⚠️ **GitHub.com Fallback** ⚠️