【问题标题】:Accepting ip address as Input from user接受 ip 地址作为用户的输入
【发布时间】:2014-07-15 21:03:47
【问题描述】:
import os
import sys
import time
import pprint
import subprocess
from netaddr import *

print "(1).Ping specific target.\n(2).Ping sweep Subnet.\n(3).Exit"
choice = raw_input("Enter your choice:- ")
if choice == '1':
    host = raw_input("Enter IP address to scan: ")
    ip = IPAddress("host")
    print "accepted"

这是程序的第一部分。我在接受用户的 IP 地址作为输入时遇到问题。 执行后出现以下错误。

Traceback (most recent call last):
  File "ping.py", line 13, in <module>
    ip = IPAddress("host")
  File "/usr/local/lib/python2.7/dist-packages/netaddr/ip/__init__.py", line 308, in __init__
    'address from %r' % addr)
netaddr.core.AddrFormatError: failed to detect a valid IP address from 'host'

使用python 2.7.6

【问题讨论】:

    标签: python-2.7 networking ip ip-address user-input


    【解决方案1】:

    你可以使用 try / except 循环:

    done = False
    while not done:
        host = raw_input("Enter IP address to scan: ")
        try:
            ip = IPAddress("host")
            done = True
        except netaddr.core.AddrFormatError:
            print 'Invalid IP Address Format, please try again!'
    

    【讨论】:

      【解决方案2】:

      您在分配中使用的是字符串"host",而不是变量host

      变化:

      ip = IPAddress("host")
      

      ip = IPAddress(host) 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-10-07
        • 1970-01-01
        • 2010-09-16
        • 1970-01-01
        • 2013-11-02
        • 2012-01-09
        • 1970-01-01
        相关资源
        最近更新 更多