[Python] python编写的一个简单的扫描端口的程序 →→→→→进入此内容的聊天室

来自 , 2020-02-21, 写在 Python, 查看 106 次.
URL http://www.code666.cn/view/0fe6a948
  1. #-------------------------------------------------------------------------------
  2. # Name:        PortScan
  3. # Purpose:     扫描目标主机的端口开放情况
  4. #
  5. # Author:      xxh
  6. #
  7. # Created:     05-12-2011
  8. # Copyright:   (c) xxh 2011
  9. # Licence:     <your licence>
  10. #-------------------------------------------------------------------------------
  11. #!/usr/bin/env python
  12.  
  13. import socket
  14.  
  15. def main():
  16.     sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  17.     sk.settimeout(1000)
  18.     ip=input('请输入目标主机:(默认:127.0.0.1)')
  19.     if ip=='':
  20.         ip='127.0.0.1'
  21.  
  22.  
  23.  
  24.     s=input('请输入目标主机开始端口:(默认:80)')
  25.     if s=='':
  26.         startport=80
  27.     else:
  28.         startport=int(s)
  29.  
  30.     s=input('请输入目标主机结束端口:(默认:80)')
  31.     if s=='':
  32.         endport=80
  33.     else:
  34.         endport=int(s)
  35.  
  36.     for port in range(startport,endport+1):
  37.         print('正在扫描端口:%d' % port)
  38.         try:
  39.             sk.connect((ip,port))
  40.             print('Server %s port %d OK!' % (ip,port))
  41.         except Exception:
  42.             print('Server %s port %d is not connected!' % (ip,port))
  43.     sk.close()
  44.  
  45.  
  46. if __name__ == '__main__':
  47.     main()
  48. #//python/8924

回复 "python编写的一个简单的扫描端口的程序"

这儿你可以回复上面这条便签

captcha