Python Cookbook Authors: Martelli A.,  Ascher D. Published year: 2005 Pages: 191-192/346

### 10.6 Converting IP Addresses

Credit: Alex Martelli, Greg Jorgensen

#### 10.6.1 Problem

You need to convert IP addresses from dotted quads to long integers and back, and extract network and host portions of such addresses.

#### 10.6.2 Solution

The socket and struct modules let you easily convert long integers to dotted quads and back:

```import socket, struct

def dottedQuadToNum(ip):
"convert decimal dotted quad string to long integer"
return

struct.unpack('>L',socket.inet_aton(ip))[0]

def numToDottedQuad(n):
"convert long int to dotted quad string"
return

socket.inet_ntoa(struct.pack('>L',n))

```

To split an IP address into network and host portions, we just need to apply a suitable binary mask to the long integer form of the IP address:

```def makeMask(n):
"return a mask of n bits as a long integer"
return (2L<<n-1)-1

def ipToNetAndHost(ip, maskbits):
"return tuple (network, host) dotted-quad addresses given IP and mask size"
# by Greg Jorgensen

n = dottedQuadToNum(ip)
m = makeMask(maskbits)

host = n & m
net = n - host

return numToDottedQuad(net), numToDottedQuad(host)
```

#### 10.6.3 Discussion

The format we use for the struct.pack and struct.unpack calls must start with a '>' , which specifies big-endian byte order. This is the network byte order used by the socket.inet_aton and socket.inet_ntoa functions. If you omit the '>' , struct instead uses the native byte order of the machine the code is running on, while the socket module still uses big-endian byte order.

The network part of an IP address used to be expressed as an explicit bit mask ( generally also in dotted-quad form) such as:

```'192.168.23.0/255.255.255.0'
```

However, the bit mask invariably had a certain number ( N ) bits that were 1 followed by 32- N bits that were 0. The current form, which is much more compact and readable, is therefore structured like:

```'192.168.23.0/24'
```

The part after the / is just N : the number of bits that must be 1 in the mask. If you know about a network that is expressed in this form, you can use the functions in this recipe to check if a host is within that network:

```def isHostInNet(host_ip, net_ip_with_slash):
net_ip, mask_length = net_ip_with_slash.split('/')
mask_length = int(mask_length)
net_net, net_host = ipToNetAndHost(net_ip, 32-mask_length)
assert net_host == '0.0.0.0'
host_net, host_host = ipToNetAndHost(host_ip, 32-mask_length)
return host_net == net_net
```

Note that the slash format of network addresses gives the number of bits in the mask for the network, although we wrote the ipToNetAndHost function to take the number of bits in the mask for the host. Therefore, we pass 32-mask_length in the two calls that isHostInNet makes to the ipToNetAndHost function. The assert statement is not strictly necessary, but it does assure us that the argument passed as net_ip_with_slash is correct, and (as assert statements usually do) serves as a general sanity check for the proceedings .

#### 10.6.4 See Also

Documentation for the standard library modules socket and struct in the Library Reference .

### 10.7 Grabbing a Document from the Web

Credit: Gisle Aas

#### 10.7.1 Problem

You need to grab a document from a URL on the Web.

#### 10.7.2 Solution

urllib.urlopen returns a file-like object, and you can call read on it:

```from urllib import urlopen

doc = urlopen("http://www.python.org").read(  )
print doc
```

#### 10.7.3 Discussion

Once you obtain a file-like object from urlopen , you can read it all at once into one big string by calling its read method, as I do in this recipe. Alternatively, you can read it as a list of lines by calling its readlines method or, for special purposes, just get one line at a time by calling its readline method in a loop. In addition to these file-like operations, the object that urlopen returns offers a few other useful features. For example, the following snippet gives you the headers of the document:

```doc = urlopen("http://www.python.org")
print doc.info(  )
```

such as the Content-Type : header ( text/html in this case) that defines the MIME type of the document. doc.info returns a mimetools .Message instance, so you can access it in various ways without printing it or otherwise transforming it into a string. For example, doc.info( ).getheader('Content-Type') returns the 'text/html' string. The maintype attribute of the mimetools.Message object is the 'text' string, subtype is the 'html' string, and type is also the 'text/html' string. If you need to perform sophisticated analysis and processing, all the tools you need are right there. At the same time, if your needs are simpler, you can meet them in very simple ways, as this recipe shows.

#### 10.7.4 See Also

Documentation for the standard library modules urllib and mimetools in the Library Reference .

 Python Cookbook Authors: Martelli A.,  Ascher D. Published year: 2005 Pages: 191-192/346