简体   繁体   English

在Python中使用IP地址和拆分

[英]Using IP address and splitting in Python

HELP! 救命! I'm stupid stuck and can't find how to do this. 我很愚蠢,无法找到方法。 Also because I am a beginner in Python. 也因为我是Python的初学者。

I need help with this script in Python. 我在Python中需要有关此脚本的帮助。

  1. How can I used an IP address in a python script? 如何在python脚本中使用IP地址?
  2. How can I split an IP address based on a dash (-) 如何基于破折号(-)分割IP地址

I am trying to do this 我正在尝试这样做

#code
a = input("Enter ip address: ")
print 'edit "ip-' + str(a) + '"\n',

# Shell

Enter ip address:10.203.1.10
# output
edit "ip-10.203.1.10"

How can a IP address be stored? IP地址如何存储? When I add the decimal points I can get an error Is there a module or package or some secret. 当我加上小数点时,我会得到一个错误:是否存在模块或软件包或某个秘密。 Can the split method work like this x.split("-") to split and IP address how do you store the values then (a,b)? 拆分方法是否可以像x.split(“-”)那样进行拆分并分配IP地址,然后如何存储(a,b)的值? to have something like this. 有这样的事情。

input 输入

10.228.50.88-10.228.50.91

output 输出

edit "ipr-10.228.50.88-10.228.50.91" 编辑“ ipr-10.228.50.88-10.228.50.91”

set type iprange 设置类型iprange

set start-ip 10.228.50.88 设置开始IP 10.228.50.88

set end-ip 10.228.50.91 设置端IP 10.228.50.91

Really appreaciate any help or anyone that can point me in the right direction. 真正感谢任何帮助或任何可以向我指出正确方向的人。

I hope below script will help you: 我希望以下脚本可以为您提供帮助:

a = raw_input("Enter ip address: ")

print 'edit "ip-' + str(a) + '"\n'

if '-' in a:
    ips = a.split('-')

    print ('set type iprange')

    print ('set start-ip '+ips[0])
    print 'set end-ip '+ips[1]

The problem is that you're using input while running this on Python 2. You expect its return type to be a string, but it's not. 问题是在Python 2上运行时正在使用input 。您希望它的返回类型是字符串,但不是。 In Python 2, input actually tries to parse and execute the input as if it was Python code . 在Python 2中, input实际上试图解析和执行输入,就好像它是Python代码一样 Obviously, 192.168.0.0 , or any IP address, is invalid Python code, so you get an error. 显然, 192.168.0.0或任何IP地址是无效的Python代码,因此会出现错误。

To solve this, use raw_input instead of input . 要解决此问题,请使用raw_input而不是input Then, a would be of type str , that is, a regular string (there's nothing special about IP addresses), so you can use a.split , a.join and other methods of the str class. 然后, a的类型将为str ,即常规字符串(IP地址没有特殊要求),因此可以使用str类的a.splita.join和其他方法。

Per https://docs.python.org/2/library/functions.html#input , input is doing this: eval(raw_input(prompt)) . 根据https://docs.python.org/2/library/functions.html#input ,输入正在执行此操作: eval(raw_input(prompt)) The eval means it is trying to take the input and interpret it as python code, which is why you are getting syntax errors. eval表示它试图获取输入并将其解释为python代码,这就是为什么您遇到语法错误的原因。 It also says: Consider using the raw_input() function for general input from users, which sounds like what you are looking for. 它还说:考虑将raw_input()函数用于用户的常规输入,这听起来像您在寻找什么。 Also, a quick test and from the Python3 doc, it looks like the input() functionality is different and behaves more like the raw_input() function. 另外,通过Python3文档进行的快速测试,看起来input()的功能有所不同,其行为更类似于raw_input()的功能。 Quick tests: 快速测试:

Python 3.6.2: Python 3.6.2:

>>> a = input("Enter ip address: ")
Enter ip address: 10.1.1.1
>>> a
'10.1.1.1'

Python Python 2.7.13: Python Python 2.7.13:

>>> a = input("Enter ip address: ")
Enter ip address: 10.1.1.1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    10.1.1.1
     ^
SyntaxError: invalid syntax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM