Home > Software design >  getting errors.. Using Socket in python
getting errors.. Using Socket in python

Time:06-28

This is the task:

Your task is to write a Python program that should test the given host and it should generate a list of all the TCP Open ports within the range of 1 to 1025. You are required to accomplish this task by using standard Python’s “socket” library. Following are the functional requirements:

  1. On execution of program system should prompt “Enter a host to scan”. User will provide a host name

  2. System should look for all the ports between the range of 1 to 1025

  3. If the Ports is open it should create a file and add an entry for port number

  4. In case of any exception for instance “host is not available”, “host name could not be resolved” or due to any other error you need to write that exception into same file

  5. You also need to record starting and ending date and time at the beginning and ending of file accordingly. It should also show the total time it took in port scanning process.

This is the source code that I wrote with the help of online resources (also attaching screenshots of code):

import socket
#Syntax for creating a socket
sock = socket.socket (socket_family, socket_type)

#Creates a stream socket
sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM)

#AF_INET 
#Socket Family (here Address Family version 4 or IPv4) 

#SOCK_STREAM Socket type TCP connections 

#SOCK_DGRAM Socket type UDP connections 

#Translate a host name to IPv4 address format 
gethostbyname("host") 

#Translate a host name to IPv4 address format, extended interface
socket.gethostbyname_ex("host")  

#Get the fqdn (fully qualified domain name)
socket.getfqdn("8.8.8.8")  

#Returns the hostname of the machine..
socket.gethostname()  

#Exception handling
socket.error



#!/usr/bin/env python
import socket
import subprocess
import sys
from datetime import datetime

# Clear the screen
subprocess.call('clear', shell=True)

# Ask for input
remoteServer    = raw_input("Enter a remote host to scan: ")
remoteServerIP  = socket.gethostbyname(remoteServer)

# Prints a banner with information on which host we are about to scan
print ("-" * 60)
print ("Please wait, scanning remote host", remoteServerIP)
print ("-" * 60)

# Check what time the scan started
t1 = datetime.now()

# Using the range function to specify ports (here it will scans all ports between 1 and 1024)

# We also put in some error handling for catching errors

try:
    for port in range(1,1025):  
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((remoteServerIP, port))
        if result == 0:
            print ("Port {}:      Open".format(port))
        sock.close()
    
except KeyboardInterrupt:
    print ("You pressed Ctrl C")
    sys.exit()

except socket.gaierror:
    print ('Hostname could not be resolved. Exiting')
    sys.exit()

except socket.error:
    print ("Couldn't connect to server")
    sys.exit()

# Checking the time again
t2 = datetime.now()

# Calculates the difference of time, to see how long it took to run the script
total =  t2 - t1

# Printing the information to screen
print ('Scanning Completed in: ', total)

I am getting this error:

Traceback (most recent call last):
  File "C:\Users\Uday\Desktop\Tempo\socket project.py", line 3, in <module>
    sock = socket.socket (socket_family, socket_type)
NameError: name 'socket_family' is not defined
>>> 

How do I solve this problem?

CodePudding user response:

Looks like the third line in your code is the problem, you should comment it out with a #

CodePudding user response:

You will need to specify values for those 2 parameters when creating your socket.socket

By looking at the rest of your source code you would want to use socket.AF_INET for your socket_family and socket.SOCK_STREAM for socket_type. This tells the system to open a "TCP" socket using the "IPv4" address family.

Since you already have that in the line below, my simplest explanation would be that you just forgot to place the comment expression # in that line ;)

  • Related