I have been looking for tutorials for about an hour now for pythin networking and nothing i am finding is helpful.

What i need is a python program that will take a computers ip adress and tell me if a specific port is open.

Can anyone give me a quick, up to date example that will do this? I do not need multi-threading or anything fancy. Just a quick straight to the point program. Thanks.

Import socket

target_ip = ""

target_port = ""

# where do i go from here? Thanks.

Recommended Answers

All 5 Replies

At least type import lower case as python is case sensitive.

I so far have this. And yes, i should probably do that but i was just typing it quickly ;)

import socket

while True:

        for port in range(2000,2020):

            address = "localhost"
    
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

            try:
                s.connect((address, port))
            except socket.error:
                print "%d: closed" % port + "\n"
                continue
            
            print "%d: open" % port
            s.close()

         break

but this keeps saying all my ports are closed. How do i open one so i can toy with it and how would i go about toying with it? Thanks

import socket

socket.create_connection(("localhost", "2000"))

print "done"

error: [Errno 10061] No connection could be made because the target machine actively refused it

Why does my machine refuse the connection? how do i get around this? Thanks

import socket

socket.create_connection(("localhost", "2000"))

print "done"

error: [Errno 10061] No connection could be made because the target machine actively refused it

Why does my machine refuse the connection? how do i get around this? Thanks

Why don't you run the example servers and clients programs in the documentation of the socket and the SocketServer module ?

You can open ports by binding a server to them.

#create socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#bind to localhost at port 1002
s.bind(('',1002))

while 1:
    #accept up to 5 connections
    s.listen(5)
    #get connection object and address
    conn, addr = s.accept() 
    #grab the data if any
    data = conn.recv(100)

That would open up port 1002.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.