- #-------------------------------------------------------------------------------
- # Name: PortScan
- # Purpose: 扫描目标主机的端口开放情况
- #
- # Author: xxh
- #
- # Created: 05-12-2011
- # Copyright: (c) xxh 2011
- # Licence: <your licence>
- #-------------------------------------------------------------------------------
- #!/usr/bin/env python
- import socket
- def main():
- sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sk.settimeout(1000)
- ip=input('请输入目标主机:(默认:127.0.0.1)')
- if ip=='':
- ip='127.0.0.1'
- s=input('请输入目标主机开始端口:(默认:80)')
- if s=='':
- startport=80
- else:
- startport=int(s)
- s=input('请输入目标主机结束端口:(默认:80)')
- if s=='':
- endport=80
- else:
- endport=int(s)
- for port in range(startport,endport+1):
- print('正在扫描端口:%d' % port)
- try:
- sk.connect((ip,port))
- print('Server %s port %d OK!' % (ip,port))
- except Exception:
- print('Server %s port %d is not connected!' % (ip,port))
- sk.close()
- if __name__ == '__main__':
- main()
来源: http://www.phpxs.com/code/1005157/