The example in this article describes how Python detects TCP ports on remote servers. Share to everyone for your reference. Specific as follows:
Python detects the code of the remote server TCP port, which can be used as a server monitoring utility
#!/usr/bin/env python #coding: utf-8 #filename: tcp.py ' Author:gavingeng date: 2011-12-14 09:35:59 ' Import Socke T import sys normal=0 error=1 timeout=5 def Ping (ip,port,timeout=timeout): try: cs=socket.socket (socket.af_ Inet,socket. SOCK_STREAM) address= (str (IP), int (port)) status = CS.CONNECT_EX (address) Cs.settimeout (timeout) #this status is returnback from TCPServer if status! = NORMAL: print ERROR else: print normal
except Exception, E: print ERROR print "error:%s"%e return error return NORMAL if __name__== ' __ Main__ ': If Len (SYS.ARGV) < 3: print ur ' Please use in the following format:./tcp.py www.jb51.net ' sys.exit (1) IP = SYS.ARGV[1] port = sys.argv[2] try: timeout = sys.argv[3] except Indexerror, E: timeout= TIMEOUT Ping (ip,port,timeout)
Hopefully this article will help you with Python programming.