Python code for detecting TCP port of remote server

From , 4 Years ago, written in Python, viewed 54 times.
URL https://pastebin.vip/view/6e4243f5
  1. #!/usr/bin/env python  
  2. #coding:utf-8  
  3. #filename:tcp.py  
  4.  
  5. '''  
  6. author: gavingeng  
  7. date:   2011-12-14 09:35:59  
  8. '''  
  9. import socket  
  10. import sys  
  11.  
  12. NORMAL=0  
  13. ERROR=1  
  14. TIMEOUT=5  
  15.  
  16. def ping(ip,port,timeout=TIMEOUT):  
  17.     try:  
  18.         cs=socket.socket(socket.AF_INET,socket.SOCK_STREAM)  
  19.         address=(str(ip),int(port))  
  20.         status = cs.connect_ex((address))  
  21.         cs.settimeout(timeout)  
  22.         #this status is returnback from tcpserver  
  23.         if status != NORMAL :  
  24.             print ERROR  
  25.         else:  
  26.             print NORMAL      
  27.     except Exception ,e:  
  28.         print ERROR  
  29.         print "error:%s" %e  
  30.         return ERROR  
  31.      
  32.     return NORMAL  
  33.  
  34. if __name__=='__main__':  
  35.     if len(sys.argv) < 3 :  
  36.         print ur'请按照如下格式使用: ./tcp.py www.sharejs.com 80'  
  37.         sys.exit(1)  
  38.      
  39.     ip = sys.argv[1]  
  40.     port = sys.argv[2]  
  41.     try:  
  42.         timeout = sys.argv[3]  
  43.     except IndexError ,e:  
  44.         timeout=TIMEOUT  
  45.     ping(ip,port,timeout)  
  46. #//python/8923

Reply to "Python code for detecting TCP port of remote server"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website