Python 3 将 IPV4Address 转换为字符串
Python 3 Convert IPV4Address to string
我正在尝试根据以下错误消息将 python3 中的 IPV4Address 转换为 string
或 int
。我不太确定我在这里做错了。我也试过 int(x)
和 bytes(x)
都不行
Traceback (most recent call last):
File "/Users/krisarmstrong/PycharmProjects/pyportscanner/pyportscanner", line 113, in <module>
main()
File "/Users/krisarmstrong/PycharmProjects/pyportscanner/pyportscanner", line 105, in main
get_ip_address()
File "/Users/krisarmstrong/PycharmProjects/pyportscanner/pyportscanner", line 70, in get_ip_address
result = sock.connect_ex((x, port))
TypeError: str, bytes or bytearray expected, not IPv4Address
我的代码如下:
def get_ip_address():
ip_address_list = []
port = '2359'
network = ipaddress.ip_network('10.0.16.0/20')
for x in network.hosts():
str(x)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((x, port))
if result == 0:
print("Port"), port(" Open")
sock.close()
ip_address_list.append(x)
return ip_address_list
在您的代码片段(下面的相关行)中,str(x)
行 returns x
作为字符串但确实将此字符串存储在 x
中。您需要执行 x = str(x)
以将字符串表示形式存储在 x
中(或者在调用 connect_ex
时仅使用 str(x)
代替 x
)。
for x in network.hosts():
str(x)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((x, port))
你忘了把str(x)的值赋给x,更正后的版本是:
for x in network.hosts():
x = str(x)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((x, port))
我正在尝试根据以下错误消息将 python3 中的 IPV4Address 转换为 string
或 int
。我不太确定我在这里做错了。我也试过 int(x)
和 bytes(x)
都不行
Traceback (most recent call last):
File "/Users/krisarmstrong/PycharmProjects/pyportscanner/pyportscanner", line 113, in <module>
main()
File "/Users/krisarmstrong/PycharmProjects/pyportscanner/pyportscanner", line 105, in main
get_ip_address()
File "/Users/krisarmstrong/PycharmProjects/pyportscanner/pyportscanner", line 70, in get_ip_address
result = sock.connect_ex((x, port))
TypeError: str, bytes or bytearray expected, not IPv4Address
我的代码如下:
def get_ip_address():
ip_address_list = []
port = '2359'
network = ipaddress.ip_network('10.0.16.0/20')
for x in network.hosts():
str(x)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((x, port))
if result == 0:
print("Port"), port(" Open")
sock.close()
ip_address_list.append(x)
return ip_address_list
在您的代码片段(下面的相关行)中,str(x)
行 returns x
作为字符串但确实将此字符串存储在 x
中。您需要执行 x = str(x)
以将字符串表示形式存储在 x
中(或者在调用 connect_ex
时仅使用 str(x)
代替 x
)。
for x in network.hosts():
str(x)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((x, port))
你忘了把str(x)的值赋给x,更正后的版本是:
for x in network.hosts():
x = str(x)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex((x, port))