我试图编写一个简单的端口扫描程序,或者" telnet test"在Windows上使用Python 3。
不幸的是,我在运行代码时遇到以下错误。
输出
C:\Python\Codes>python test.py
Destination IP Address: 127.0.0.1
Port number: 2020
Host 127.0.0.1 port 2020
Traceback (most recent call last):
File "test.py", line 12, in <module>
SCAN_TCP_PORT()
File "test.py", line 9, in SCAN_TCP_PORT
s.connect((DstIP, DstPort))
TypeError: an integer is required (got type str)
C:\Python\Codes>
我一直在使用Google搜索并找到此链接,但它与我的问题无关。
Why is Python giving me "an integer is required" when it shouldn't be?
第9行的代码出了什么问题?以及如何解决它?
错误
File "test.py", line 9, in SCAN_TCP_PORT
s.connect((DstIP, DstPort))
TypeError: an integer is required (got type str)
代码
#!/usr/bin/env python
import sys, os, socket
def SCAN_TCP_PORT():
DstIP = input('\nDestination IP Address: ')
DstPort = input('Port number: ')
print ('Host %s port %s' % (DstIP, DstPort))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((DstIP, DstPort))
print("%s is listening on TCP port %s" % (DstIP, DstPort))
SCAN_TCP_PORT()
答案 0 :(得分:1)
您需要将DstPort的值转换为整数,例如将s.connect((DstIP, DstPort))
替换为s.connect((DstIP, int(DstPort)))