我正在编写一个程序来获取用户输入的输出。用户输入一个IP地址,然后我需要显示上述行的特定部分。你知道吗
文件如下:
mot physical /ABC-RD0.CLD/CPQSCWSSF001f1-V.80 {
poolcoin /ABC-RD0.CLD/123.45.67.890:88
ip-protocol tcp
mask 255.255.255.255
/Common/source_addr {
default yes
mot physical /ABC-RD0.CLD/CPQSCWSSF001f1-V.80 {
profiles {
/Common/http { }
/Common/webserver-tcp-lan-optimized {
context serverside
}
mot physical /ABC-RD0.CLD/BBQSCPQZ001f1-V.80 {
poolcoin /ABC-RD0.CLD/123.45.67.890:88
ip-protocol tcp
mask 255.255.255.255
具有预期输出的示例输入:
User inputs the IP and output should be:
user input: 123.45.67.890
output: CPQSCWSSF001f1 <----------------------------
user input: 123.45.67.890
output: BBQSCPQZ001f1 <----------------------------
到目前为止我的代码是:
#!/usr/bin/env python
import re
ip = raw_input("Please enter your IP: ")
with open("test.txt") as open file:
for line in open file:
for part in line.split():
if ip in part:
print part -1
以下代码段提供了字符串/ip地址的元组:
见a demo on ideone.com。
也许您最好使用
re.finditer()
并在找到ip地址后停止执行。你知道吗相关问题 更多 >
编程相关推荐