Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Code to check IP address in XML file  RSS feed

 
Prasann Mathur
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an XML file like this . I want to check an IP , say 172.31.1.47 or 192.39.1.89 which is the IP of the client and check whihc range (defined in begin and end tag of nodes) .

Say for an example, Ip 172.31.1.47 falls inside <begin> and<end> value 172.31.1.1 - 172.31.1.254 once it verifies these, it will retrieve all the other attributes of the node.

<?xml version="1.0"?>

<project>
<AppA>
<begin>172.31.1.1</begin>
<end>172.31.1.254</end>
<attribute1>275</attribute1>
</AppA>
<AppB>
<begin>192.39.1.1</begin>
<end>192.39.1.254</end>
<attribute1>285</attribute1>
</AppB>
</project>

Tell me is there any logic to check whether an Ip falls under the begin and end tag Ip address values.
 
joseph edwards
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can convert the ip address in to a 32-bit value. For instance, each tuple in the IP address will be represented as one byte. Once converted, you can do simply integer comparison to see whether a specific IP address falls within a specific range.

For example:

192.168.111.200 = 11000000 10101000 11011111 11001000

Originally posted by Prasann Mathur:
I have an XML file like this . I want to check an IP , say 172.31.1.47 or 192.39.1.89 which is the IP of the client and check whihc range (defined in begin and end tag of nodes) .

Say for an example, Ip 172.31.1.47 falls inside <begin> and<end> value 172.31.1.1 - 172.31.1.254 once it verifies these, it will retrieve all the other attributes of the node.

<?xml version="1.0"?>

<project>
<AppA>
<begin>172.31.1.1</begin>
<end>172.31.1.254</end>
<attribute1>275</attribute1>
</AppA>
<AppB>
<begin>192.39.1.1</begin>
<end>192.39.1.254</end>
<attribute1>285</attribute1>
</AppB>
</project>

Tell me is there any logic to check whether an Ip falls under the begin and end tag Ip address values.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!