Python调用socket.bind()传2个参数报错仅接受1个如何解决
问题原因
Python标准库socket模块的bind()方法仅接受1个参数,该参数为包含绑定地址与端口的元组,不能分开传入IP、端口两个独立参数。另外你原有代码中传入参数的顺序也有误,元组内需要先写IP地址,再写端口。
修复方案
将原有绑定套接字的代码行:
socket_connection.bind(port,ip_add)
修改为:
socket_connection.bind((ip_add, port))
另外你现有代码中两处格式化打印的字符串没有预留占位符,format()参数无法正常插入,同时为了避免套接字初始化失败时finally块调用close()报错,建议同步做对应调整。
完整修正后代码
from socket import * def main (): print('''========================================================================== ''') print(''' [-] S T A R T I N G T H E D E C O Y S E R V E R [-] ''') print('''========================================================================== ''') ip_add = "192.168.43.195" port = 80 socket_connection = None try : socket_connection=socket(AF_INET,SOCK_STREAM) # 修正绑定逻辑,传入IP+端口的元组 socket_connection.bind((ip_add, port)) socket_connection.listen(10) while 1 : attacker_connection,attacker_addr = socket_connection.accept() # 补充格式化占位符 print("Decoy Server Is Created on The Attacker: {}".format(attacker_addr[0])) attacker_connection.send(b"<h1> Unable to connect from The server Please Check Your Network Connection </h1>") data = attacker_connection.recv(3072) print(data) except error as identifier: # 补充格式化占位符 print("An Error Was Encounterd While Deploying A Decoy Server: {}".format(identifier)) except KeyboardInterrupt as ky : print("Decoy Server Stopped By The User") print(" [+]..../ Exiting \....[+] ") finally: if socket_connection: socket_connection.close() if __name__ == '__main__': main()
内容的提问来源于stack exchange,提问作者Mohamed Thoufeeq
相关产品推荐
相关产品推荐

