Ask a question to Desklib · AI bot


Connecting Python Programs to the Outer World: Ride Across the River Background

12 Pages5858 Words305 Views

Added on  2019-09-24

About This Document

This project eliminates the need to design one's own protocol (where the standard protocols will suffice, which is more often than you might think) and allows programs to be combined in arbitrary ways; as long as they support the protocol, they've taken a big step toward being able to interoperate with each other.
BookmarkShareRelated Documents
Project #3: Ride Across the RiverBackgroundWe saw in the previous project that our Python programs are capable of connecting to the "outside world" around them — to other programs running on the same machine, or even to other programs running on different machinesin faraway places. This is a powerful thing for a program to be able to do, because it is no longer limited to taking its input from a user or from a file stored locally; its input is now potentially anything that's accessible via the Internet, making it possible to solve a vast array of new problems and process a much broader collection of information. Once you have the ability to connectyour programs to others, a whole new world opens up: suddenly, the idea that you should be able to write a program that combines, say, Google search queries, the Internet Movie Database, and your favorite social network to find people who like movies similar to the ones you like doesn't seem so far-fetched.But we also saw that getting programs to share information is tricky, for (at least) two reasons. Firstly, there's a software engineering problem: A protocol has to be designed that both programs can use to have their conversation. Secondly, there's a social problem: If the same person (or group of people) isn't writing both programs, it's necessary for them to agree on the protocol ahead of time, then to implement it. This second problem has a potentially catastrophic effect on our ability to make things work — how could you ever convince Google to agree to use your protocol just to communicate with you?In practice, both of these problems are largely solved by the presence ofstandards, such as those defined by theWorld Wide Web Consortium. Standards help by providing detailed communication protocols whose details have already been hammered out, with the intention of handling the most common set of needs that will arise in programs. This eliminates the need to design one's own protocol (where the standard protocols will suffice, which is more often than you might think) and allows programs to be combined in arbitrary ways; as long as they support the protocol, they've taken a big step toward being able to interoperate with each other. What's more, standard protocols often have standard implementations, so that you won't have to code up the details yourself as you did in the previous project. For example, Python has built-in support for a number of standard Internet protocols, including HTTP (HyperText Transfer Protocol, the protocol that your browser uses to download web pages) among others.At first blush, HTTP doesn't seem all that important. It appears to be a protocol that will allow you to write programs that download web pages (i.e., that allow you to write programs that play the same role that web browsers do). But it turns out that HTTP is a lot more important thanthat,since it is the
Connecting Python Programs to the Outer World: Ride Across the River Background_1
protocol that underlies a much wider variety of traffic on the Internet than you might first imagine. This is not limited only to the conversation that your browser haswitha web server in order to download a web page, though that conversation most often uses HTTP (or its more secure variant, HTTPS). HTTP also underlies a growing variety of program-to-program communications using web protocols, whereweb sitesor other software systems communicate directly with what are broadly calledweb services, fetching data and affecting change. This is why you can post tweets to Twitter using either theirweb site, a client application on your laptop, or a smartphoneapp; all of these applications use the same protocol to communicate with the Twitter service, differing only in the form of user interface they provide.Fortunately, since HTTP support is built directly into Python, we can write programs that use these web services without having to handle low-level details of the protocol, though there are some details that you'll need to be familiar with if you want to use the provided implementation effectively. We'llbe discussing some of these details in lecture soon, and these will be accompanied by acode example, which will give you some background in the tools you'll need to solve these kinds of problems in Python.This project gives you the opportunity to explore a small part of the vast sea ofpossibilities presented by web APIs and web services. You'll likely find that you spend a lot of your time in this project understanding the web API you'll need — being able to navigate technical documentation and gradually build anunderstanding of another system is a vital skill in building real software — andthat the amount of code you need isn't nearly what you might expect when youfirst read the project write-up. As always, work incrementally rather than trying to work on the entire project all at once. When you're done, you'll have taken a valuable step toward being able to build Python programs that interact with web services, which opens up your ability to write programs for yourself that are real and useful.Additionally, you'll get what might be your first experience with writing classes in Python, which will broaden your ability to write clean, expressive Python programs, a topic we'll continue revisiting and refining throughout the rest of this course.The problem, in generalIn your work on this project, you'll write a program that is capable of displaying information about a trip from one location to another (e.g., driving directions between two street addresses). For example, you might display turn-by-turn directions, an estimate of how long it might take to get from one location to another, and so on. You'll use real-world map data — real cities, real streets — to solve your problem. So, when you're done, your program willbe a very simple navigation system, not entirely unlike the ones you see on
Connecting Python Programs to the Outer World: Ride Across the River Background_2
some smartphones or in some cars. If you want to know how to drive from Bren Hall at UCI to Staples Center in Los Angeles, your program will be able to tell you.That may sound like something that is well beyond your current skill level and/or time you have available, that you're being asked to build a complete, professional system that would ordinarily be written by a large team of people over a period of months. And, indeed, if you had to write the entire system from scratch, that would certainly be true; it would take even a seasoned professional a lot longer than the time allotted to complete a task like that, and would require skills well beyond what's been taught in your coursework to date.But there is good news here: We operate in an interconnected world, where information of all kinds is available to us in web browsers, smartphone applications, and so on. And we're fortunate that a lot of that information is available for free, not just in a way that lets us view it in a web browser, but in a way that makes it available to the programs we write. Provided that we can find an online service that provides the information we need, and provided thatwe're licensed to use it — either because it's free and we meet the terms of use,or because we're willing to pay for the privilege — we can use it to solve our problem.As it turns out, the information we need to solve this particular problem is available online, in a form that can be consumed by a Python program, and licensed in a way that lets us use it (because it is free for non-commercial use).The only trick is figuring out how to get the information into our program. Luckily for us, the service that provides this information uses standard protocols and formats that are common on the Internet; even better, all of theseprotocols and formats are implemented already in Python's standard library, sothe low-level details will not be our concern, and we can focus on the more interesting parts of the problem.The MapQuest Open Data APIsMapQuest is a company that is in the business of providing online services for displaying maps, providing directions, reporting on current traffic conditions, and other related services. While they're a for-profit company, some of their services are provided free of charge for non-commercial use. As long as you'renot building a product from which you'll be trying to make money, you can use MapQuest's free services, with the one additional caveat that you have to follow the rules laid out in their license. These kinds of license restrictions are no joke, so we'll spend a little time to be sure we're taking them seriously.For our work on this project, you'll be concerned with two parts of the Open MapQuest API, provided by a company called MapQuest; we'll need both the Open Directions Service and the Open Elevation Service. Both of these are
Connecting Python Programs to the Outer World: Ride Across the River Background_3
web-based APIs, similar to the one we used in thecode examplein which we downloaded and displayed information about YouTube videos. Like YouTube's API, the Open MapQuest API uses HTTP, with queries described using a URL, and with responses returned in JSON format. Fortunately, all of these technologies are supported in Python's standard library, so most of the details are things that will be handled for us automatically, but, as we saw in the YouTube example, there are some things we need to get right, and not all of the detailswilbe the same in the MapQuest example as they were in the YouTube example, so it'll be vital for you to understandwhywe did the things we did in thecode example, so you can know whether and where the same techniques apply.The two APIs you'll need are described in detail at the links below. You certainly won't need to read all of the documentation, but you'll want to take a look around and familiarize yourself with what the API cando,because part ofyour goal in this project is to decide what parts of the API you'll need to solve your problem.MapQuest Open Directions Service documentationMapQuest Open Elevation Service documentationCreating an account and getting an AppKeyLike YouTube's API that we saw in class, MapQuest's API requires anAPI Key, which links your usage of the API to an account and authorizes you to use the API. Before you can make use of the API, you'll need to obtain your own API Key — and due to usage restrictions, we won't all be able to share the same key, so each of you will need to take this step.Do not share your API Key with other students!You'll only need to do this once, and you'll be able to use your API Key for all of your work on this project once it's been created. And, don't worry, obtaining the API Key is free for non-commercial uses like ours.Visit theMapQuest Developersite in your browser.Part of the way down the page, you'll see a blue button that saysGet Your Free API Key. Click that button.A form will be displayed, in which you can choose a username, a password, and so on. Fill in the necessary information, and be sure to use an email address that you have access to; you'll need to receive emails from MapQuest along the way.Once you've created your account, you'll be logged in and presented with some choices, one of which isKeys & Reporting. Click that.You'll then see a green button that saysCreate a New App. Click that, because you're going to be writing an application that uses the Open MapQuest APIs.When asked, supply anApp Name(maybeICS 32 Project 3would be a good choice). TheCallback URLis not important forus,since we're not
Connecting Python Programs to the Outer World: Ride Across the River Background_4

Found this document preview useful?

Related Documents
Wireless and Network Security