×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Port Scanner Code Development Assignment Solution using Python

July 10, 2024
Dr. Sophia Nguyen
Dr. Sophia
🇸🇬 Singapore
Python
Dr. Sophia Nguyen holds a Doctorate in Computer Engineering from an esteemed university in Singapore and has completed over 900 assignments in Python file handling. Dr. Nguyen's expertise spans across areas such as file I/O optimizations, concurrency in file operations, and developing scalable file management systems. She excels in handling large datasets, implementing efficient error recovery strategies, and integrating file operations into web applications.
Key Topics
  • Problem:
Tip of the day
Ensure you understand the dataset thoroughly before starting your machine learning assignment. Visualize the data, check for missing values, and identify patterns or anomalies to guide your model-building process effectively.
News
In 2024, universities have introduced new programming courses focusing on cybersecurity, machine learning, and artificial intelligence to better prepare students for modern IT careers.

Problem:

Write a python assignment where you have to enhance the port scanner code developed in class to scan IP ports. The program should ask for the IP number, starting port number, and ending port number.

After all the requisite ports are scanned, the system should print the total time it takes to do that. (Hint: Use time module)

Output 1
Output 2

After all, ports are scanned, prints the total time taken in seconds.

Output 3

Solution:

import socket import time import sys from datetime import date if __name__ == '__main__': # ask for domain ip = input("Enter ip address or domain name to scan: ") # Validate that input is numeric for starting port end ending port try: port_a = int(input("Enter starting port number (1 - 65535): ")) port_b = int(input("Enter ending port number (1 - 65535 greater than start port num): ")) except: print("The input for starting port and ending port must be numeric.") sys.exit(1) # Check that starting port and ending port are between 1-65535 if port_a not in range(1, 65535+1) or port_b not in range(1,65535+1): print("Starting port and ending prot must be between 1-65535.") sys.exit(1) if port_b <= port_a: print("Ending port number must be greater than starting port number.") sys.exit() # print info print('*'*30) print(f"List of open ports. Host:{ip} Start Port#:{port_a} End Port#:{port_b}") print(f"Start: {date.today()}") print('*'*30) # Save starting time start_time = time.time() for port in range(port_a, port_b+1): # iterate through ports print(f"Scanning port {port}", end="\r") try: a_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) a_socket.settimeout(1) result = a_socket.connect_ex((ip, port)) if result == 0: # port open print(f"{ip}:Port:{port}") except socket.error as e: #print("time out.") print(e) finally: a_socket.close() # Save ending time end_time = time.time() # Print time taken print("Time taken {:.2f} seconds".format(end_time-start_time))

Related Samples

Explore our collection of free Python assignment samples to see examples of our expertise. These samples showcase practical applications and solutions, helping you understand our approach to Python programming assignments.