09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 1 ■ INTRODUCTION TO CLIENT/SERVER NETWORKING<br />

reply = json.loads(rawreply)<br />

print reply['Placemark'][0]['Point']['coordinates'][:-1]<br />

In this listing, all references to the idea <strong>of</strong> a URL have disappeared—in fact, none <strong>of</strong> <strong>Python</strong>’s URLrelated<br />

libraries are imported at all! Instead, we are here directly manipulating the HTTP protocol: asking<br />

it to connect to a specific machine, to issue a GET request with a path that we have constructed, and<br />

finally to read the reply directly from the HTTP connection. Instead <strong>of</strong> being able to conveniently<br />

provide our query parameters as separate keys-and-values in a dictionary, we are having to embed them<br />

directly, by hand, in the path that we are requesting by first writing a question mark (?) followed by the<br />

parameters in the format name=value and all separated by & characters.<br />

The result <strong>of</strong> running the program, however, is much the same as for the programs shown<br />

previously:<br />

$ python search3.py<br />

[-84.3063479, 41.5228242]<br />

As we will see throughout this book, HTTP is just one <strong>of</strong> many protocols for which the <strong>Python</strong> Standard<br />

Library provides a built-in implementation. In search3.py, instead <strong>of</strong> having to worry about all <strong>of</strong> the<br />

details <strong>of</strong> how HTTP works, our code can simply ask for a request to be sent and then take a look at the<br />

resulting response. The protocol details that the script has to deal with are, <strong>of</strong> course, more primitive than<br />

those <strong>of</strong> search2.py, because we have stepped down another level in the protocol stack, but at least we are<br />

still able to rely on the Standard Library to handle the actual network data and make sure we get it right.<br />

A Raw <strong>Network</strong> Conversation<br />

But, <strong>of</strong> course, HTTP cannot simply send data between two machines using thin air. Instead, the HTTP<br />

protocol must operate by using some even simpler abstraction. In fact, it uses the capacity <strong>of</strong> modern<br />

operating systems to support a plain-text network conversation between two different programs across<br />

an IP network. The HTTP protocol, in other words, operates by dictating exactly what the text <strong>of</strong> the<br />

messages will look like that pass back and forth between two hosts implementing the protocol.<br />

When we move beneath HTTP to look at what happens below it, we are dropping down to the very<br />

lowest level <strong>of</strong> the network stack that we can still access easily from <strong>Python</strong>. Take a careful look at<br />

search4.py. It makes exactly the same networking request to Google Maps as our previous three programs,<br />

but it does so by sending a raw text message across the Internet and receiving a bundle <strong>of</strong> text in return.<br />

Listing 1–4. Talking to Google Maps Through a Bare Socket<br />

#!/usr/bin/env python<br />

# <strong>Foundations</strong> <strong>of</strong> <strong>Python</strong> <strong>Network</strong> <strong>Programming</strong> - Chapter 1 - search4.py<br />

import socket<br />

sock = socket.socket()<br />

sock.connect(('maps.google.com', 80))<br />

sock.sendall(<br />

» 'GET /maps/geo?q=207+N.+Defiance+St%2C+Archbold%2C+OH'<br />

» '&output=json&oe=utf8&sensor=false HTTP/1.1\r\n'<br />

» 'Host: maps.google.com:80\r\n'<br />

» 'User-Agent: search4.py\r\n'<br />

» 'Connection: close\r\n'<br />

» '\r\n')<br />

rawreply = sock.recv(4096)<br />

print rawreply<br />

6

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!