Python methods for detecting TCP ports on remote servers

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.