I need a way to check what service a port is running in python, thanks for the help in advance.

Do you know how to do it in any other language?

if you know how to do it from CLI then try subprocess

Do you know how to do it in any other language?

I know this can be done by a command in *nix systems called nmap, i just need to do it in python, and it to be cross platform to lindows.

if you know how to do it from CLI then try subprocess

Thanks for the reply,
I only know how to do it in linux using nmap, any ideas on windows?

If you're just doing this for yourself and are not too concerned about efficiency, this should suffice:

from socket import * 

if __name__ == '__main__':
	target = "localhost"
	targetIP = gethostbyname(target)
	print 'Starting scan on host ', targetIP

	#scan reserved ports
	for i in range(20, 1025):
		s = socket(AF_INET, SOCK_STREAM)

		result = s.connect_ex((targetIP, i))

		if(result == 0) :
			print 'Port %d: OPEN' % (i,)
		s.close()

You could always consult Wikipedia's reserved port list to tell what service a user is most likely running.

Also, if you are interested in efficiency (by means of threading), just post back and I'll give you some more code.

I know this can be done by a command in *nix systems called nmap, i just need to do it in python, and it to be cross platform to lindows.

Question sustains.
Do you have a particular problem with python, or do you need help to understand portmapping in general?

Clarification: I know how to check what ports are open, I just need a way to know what service a port is running, like ftp, i have a script like SoulMazer posted, I was just hoping there was a better way of identifying what a port is running, like HTTP or FTP rather than checking the port against a list.

I was just hoping there was a better way of identifying what a port is running

AFAIK, no.

I'm not sure this is possible, but I have had some results with the following:

port = 80
proto = getservbyport(portnum)
print proto

I'm not quite sure why it returns "www", I would expect "http", but maybe Python has a way of translating it. You might have to research it yourself, but I hope this is a help.

commented: This is exactly what I needed. +1
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.