首页 文章

Python套接字错误

提问于
浏览
1
#!/usr/bin/python

import socket
import argparse
import os.path

parser = argparse.ArgumentParser(description='Scan for open ports on given 
hostname or ip address')
parser.add_argument('-H','--HOST', help ="Enter a hostname or Ip Address")
args = parser.parse_args()

    def find_port(PORT,HOST):
        data = ('\r\nSuccessful Connections were made at host {} on port {}'.format(HOST, PORT))
        originalfile = ("Data Log.txt")

       s = socket.socket()
       socket.setdefaulttimeout(1)

        try:
           s.connect((HOST, PORT))
           print '[+] Successful connection on ',PORT

        except:
           print '[+] Connection failure on port ',PORT

       if os.path.exists(originalfile) is True:
           if s.connect((HOST, PORT)):
               with open (originalfile, "w") as currentfile:
                    currentfile.write(data)
                    currentfile.close()

       elif os.path.exists(originalfile) is False:
            if s.connect((HOST, PORT)):
               with open("Data Log.txt","w+") as newfile:
                    newfile.write(data)
                    newfile.close()


    if __name__=='__main__':
       for i in range(1024):
            find_port(i, args.HOST)

我一直收到这些错误 . 所有即时尝试都是捕获端口上成功连接的实例,并将其记录在文本文件中,但我不断收到这些错误 .

[]端口0上的连接失败Traceback(最近一次调用最后一次):文件“./poo2.py”,第44行,在find_port(i,args.HOST)文件“./poo2.py”,第30行,在find_port中if s.connect((HOST,PORT)):文件“/usr/lib/python2.7/socket.py”,第228行,在meth return getattr(self._sock,name)(* args)socket.error: [Errno 99]无法分配请求的地址

1 回答

  • 0

    目前尚不清楚您要使用代码实现的目标 . 但是,您首先创建一个套接字,然后将其连接到目标,然后再尝试连接它 . 第二次连接没有意义,并且会导致错误,因为套接字只能连接一次 .

相关问题