Muhammad Suleman
Muhammad Suleman

Reputation: 797

Number of active sockets on a port?

>>import socket
>>s=socket.socket() //creates a TCP socket of address family; internet appications IPv4
>>s.bind(("0.0.0.0",8888))
>>s.listen(2)
>>client,addr=s.accept()

How can I find out the number of IPs trying to connect to the socket I created?

Upvotes: 0

Views: 961

Answers (3)

Parth Pankaj Tiwary
Parth Pankaj Tiwary

Reputation: 1

You can always have a counter and it will be incremented by one, on every connection received.

    connectionCounter = 0
    while True:
        connectionCounter += 1
        c, addr = s.accept()
        print 'Connection number: ' + str(connectionCounter)
        c.send('Thank you for your connecting')
        c.close()

I am adding a video link below to demonstrate the same. I hope it helps.

Demonstration of the above code

By using curl I am making connection request to the server.

Upvotes: 0

Muhammad Suleman
Muhammad Suleman

Reputation: 797

while True:
    c, addr = s.accept()
    print 'Got connection from', addr
    c.send('Thank you for your connecting')
    c.close()

this will indicate the connections initiated!

Upvotes: 1

zhujs
zhujs

Reputation: 553

You can try to use the netstat command to check the network connections. Can't find a way to do this in pure python code.

Upvotes: 0

Related Questions