Archive for the ‘Socket Programming’ Category

The basic idea to check if a port is open is to connect to the host at that particular port and if the connection was successful , then it means the port at which we tried to connect is open otherwise it is not open.

The first thing we do is to ask the user the IP address and the range of ports we need to scan (Alternatively you can predefine them in the source). Then we create a loop within the range of the given ports, and for each value of the port in the loop we try to connect to the current value port at the specified host, and if the connection was successful, we print that that particular port is open. And after every check we close the created socket.

# Port Scanner
# https://extr3metech.wordpress.com
from socket import *                          # Imports socket module
ip=raw_input("Enter IP to scan : ")           # Asks user to enter IP address
start=input("Enter starting port number : ")  # Asks user to enter starting port number
end=input("Enter ending port number : ")      # Asks user to enter ending port number
print "Scanning IP: " , ip
for port in range(start,end):                 # For loop from starting to ending port
    s=socket(AF_INET, SOCK_STREAM)            # Creates a socket s
    if(s.connect_ex((ip,port))==0):           # If connection to port was successful,then returns 0
        print "Port " , port, "is open"       # Prints open port
    s.close()                                 # Closes socket s
print "Scanning completed !! "

If you have any sort of queries , feel free to ask. Thank you! Happy Coding!

ΞXΤЯ3МΞ

Advertisements

Hey everyone, I would like to show you some basic network programming stuff in python (v2 or higher). This is purely for coders who have some basic knowledge in python and want to begin network programming in Python.

So, open up your Python Interpreter or Python IDE and open a new window. I am gonna show you some basic stuff you can do like how to get your host name or I.P. address using python. Of course, there is a much shorter way to write this code. I have written this code for beginners to understand what is actually going on.

___________________________________________________________________________________

# Program to print your host name

import socket                 # This  imports the socket module for using its functions

hostname=socket.gethostname()                   # This will return your hostname

print "Your Host name is : " + name

raw_input("Enter any key to continue...")

#End of Code

Save it and run it. It will print your hostname. Simple rite?? Yeah! There is your first simple program.

___________________________________________________________________________________

Now, let’s write another simple program used to resolve a given hostname to its I.P. Address. This is done by using the function socketname.gethostbyname(hostame)

# Hostname Resolver

import socket

hostname=raw_input("Enter the hostname to resolve :")

ip=socket.gethostbyname(hostname)            # Return  corresponding IP address to variable ip

print "IP ADDRESS : " + ip                   # Prints the retrieved IP Address

raw_input("Enter any key to continue.. ")


# End of Code

___________________________________________________________________________________

Happy Coding!

CheerS!!

ΞXΤRΞМΞ-X