如何使用Python提取IP地址和端口号并进行比较 [英] How to extract ip addresses and port numbers using Python and compare it

查看:643
本文介绍了如何使用Python提取IP地址和端口号并进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一个python代码。



我有一个包含字符串中的端口号和IP地址的文件。我必须提取IP地址,根据公式计算端口号,并将其与文件中的端口号进行比较,并打印出不匹配的端口号。如果ip地址是W.X.Y.Z,则端口号的公式是50000 + 200(Y)+ Z [换句话说,port = W.X.Y.Z => 50000 + 200(y)+ z]。文本文件格式如下。



exchangeA_5 = 53413; 239.189.17.13 7990



exchangeA_6 = 53415; 239.189.17.15 7990



exchangeA_e = 53470; 239.189.27.70 7990



exchangeA_5 = 53468 ; 239.189.27.68 7990



最好的方法是什么?

Need a python code for this.

I have a file containing a port numbers and ip addresses in a string. I have to extract the ip addresses, calculate the port number according to the formula and compare it to the port number in the file and print the ones that dont match. If the ip address is W.X.Y.Z the formula for port number is 50000+200(Y)+Z [in other words port = W.X.Y.Z => 50000 + 200(y) + z]. The text file is of the following format.

exchangeA_5=53413 ;239.189.17.13 7990

exchangeA_6=53415 ;239.189.17.15 7990

exchangeA_e=53470 ;239.189.27.70 7990

exchangeA_5=53468 ;239.189.27.68 7990

What is the best way to do it?

推荐答案

我想你可以解析每一行以获取名称,端口,IP。



i think you can parse each line to get the name,port,ip.

str = 'exchangeA_5=53413 ;239.189.17.13 7990'
index1 = str.find('=')
index2 = str.find(';')

elem1 = str[0:index1]
elem2 = str[index1+1:index2]
elem3 = str[index2+1:]
ip = elem3[0:elem3.find(' ')].split('.')





之后,您可以计算端口并验证它。



after that, you can calculate the port and verify it.


这篇关于如何使用Python提取IP地址和端口号并进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆