Wiley.Mobile.Python.Rapid.prototyping.of.applications.on.the.mobile.platform.Dec.2007 (779889), страница 30
Текст из файла (страница 30)
In Section 8.4, we show how to build a customserver to receive photos from the phone. In Section 9.4, we build a fullyworking application, InstaFlickr, that uploads photos to Flickr using aslightly different technique.8.2 Setting up the Development EnvironmentBefore you start writing any networked programs, you should make surethat you have a working network. Since development requires lots of trialand error, you should be able to update code on your server, as well ason your phone, without too many steps.In the following sections, we show how to set up a test environmentin four different settings:•Environment A: a WiFi-enabled phone, a local WiFi network and a PCconnected to it; the PC works as a test server and no Internet accessis needed.•Environment B: a phone with Internet access through a GSM or 3Gdata plan; the PC works as a test server and you can connect to it fromthe Internet, possibly through a firewall.•Environment C: a phone with Internet access, either through a GSMor 3G data plan or a WiFi network; you need shell (SSH) access to anexternal test server.•Environment D: a phone with Internet access, either through a GSMor 3G data plan or a WiFi network; you need access to an externalweb server which is used for testing.If you have already developed server-side code for other purposes, youcan probably use your existing environment for mobile development aswell.
If you do not have previous experience in this area, environment Ais likely to be the easiest option to start with.SETTING UP THE DEVELOPMENT ENVIRONMENT8.2.1159PreliminariesIt is useful to know what we are aiming to achieve. Here is a quickreminder of how the Internet works. Each computer or host has an IPaddress, which is of the form 123.45.54.56. Data is transferred betweenhosts in packets. Since one host can run multiple networked programs atthe same time, each program is assigned a port on which it can receivepackets.
Each packet contains the IP address and port of its destination,so the packet can find its way to the correct program at the right host.Usually, a TCP/IP connection is opened between two hosts, whichensures that the two hosts can communicate reliably with each other.The TCP protocol is built on top of the IP protocol. In particular, TCPconnections are established using IP addresses.
You can think of theconnection as a pipe through which the packets are transferred. It is thejob of a firewall to control who can open connections to which ports on ahost. These connections are the core of Internetworking. Everything elseis built on top of this, including the web.The TCP connection is similar to the Bluetooth connection establishedby the RFCOMM protocol, which we used in Chapter 7. Here, however,an IP address is used instead of a Bluetooth address and a port instead ofa channel.Our goal is to get your mobile phone, which has an IP address, toopen a connection to your server, which operates behind another IPaddress. This should be straightforward once you know both addresses.Unfortunately, your server may be behind a firewall or a Network AddressTranslation (NAT) device which hides your computer behind an IP addressof its own.
These devices may prevent the connection being opened. It ispossible to evade these obstacles but it requires further tweaking.8.2.2 Install JSON ModuleTo use the JavaScript Object Notation (JSON) protocol, which is usedin many examples in this chapter and in Chapter 9, you have to installa JSON extension module to PyS60.
This is required since JSON is notincluded in the standard PyS60 library, unlike the other modules we haveused this far.The module is installed as follows:1. Go to the book website at www.mobilepythonbook.com.2. There, conveniently on the first page, you will find a link to the JSONmodule.3. Download the file, json.py.Extension modules must be installed to the E:\Python\Lib directoryin PyS60. If this directory does not exist already, create it in a similar wayto the E:\Python\ folder, as instructed in Chapter 2.160MOBILE NETWORKINGUpload json.py to E:\Python\Lib on your phone. You can useyour usual way to upload the file. If you use an S60 2nd Edition device,choose ‘Install as Python lib module’.You can test that the module is installed correctly with the followingscript:import jsonprint "JSON OK!"If the ‘JSON OK!’ line is printed out, the module is installed correctly.The JSON module, json.py, will be needed by server-side examples aswell.
You need to copy this file to the folder on your PC where you runthe examples.8.2.3 Networking EnvironmentsFigure 8.1 summarizes the networking environment for the modernmobile phone. Typically, your phone communicates with a GSM (or3G) base station. The base station routes traffic, by way of your operator’sdata center, to the Internet. This way you can, for example, read webServersGSMInternetWiFiLocal networkFigure 8.1Networking environment of a modern mobile phoneSETTING UP THE DEVELOPMENT ENVIRONMENT161pages on your phone – assuming that your phone service includes a dataplan.Depending on your data plan, your operator may either charge youfor the amount of traffic or have a flat fee.
Many examples in this chaptergenerate less than a kilobyte of traffic, which is almost nothing nowadays;it is less than a typical web page. As long as you do not transfer sounds,images or video, it is hard to generate even a megabyte of traffic, unlessyour script generates data in an infinite loop.In the following, we give details of four network environments that aresuitable for rapid development.
If your phone has a WiFi capability, youshould probably use environment A, as it is flexible and does not costanything to use. If you plan to use GPRS or 3G instead, environment C isa suitable choice, given that you have access to a server already – if not,then environment B is a feasible option. Environment D is easy to set upbut you will have to adapt the examples to your particular environment.Environment A: Local wireless networkIf your phone has a WiFi capability, it can join a local wireless network,assuming you have such a network set up.
In the best case, you also havea laptop or other computer which can join the same local network andwhich you can use as a test server. In this case, you do not have to pay forthe traffic and you have full access to the server. You can probably avoidmany problems with firewalls too, when you operate in a local network.Environment B: Phone Internet access and a PC serverIf your phone is not WiFi-capable, your test server has to be visible tothe Internet as the traffic goes by your operator’s network. In this case,you have two options: use your local PC as a server or use a rented orco-located server.It is not always easy to use a PC as a publicly accessible server.
Youmay face problems with local firewalls and NAT devices. You shouldallow inbound traffic in your firewall to a selected port, say 9000. If youuse NAT, you should enable forwarding from port 9000 on your NATdevice to port 9000 on your local computer.In Section 8.2.4, you will see how to find out your local IP address.Your router’s or firewall’s documentation should help you here also.However, be careful with your firewall configuration; if you leave itdisabled by accident, your computer becomes easy prey for bots andcrackers.Environment C: Phone Internet access and an external test serverRenting a virtual server is inexpensive nowadays. For mobile development, you need a server with a shell account, Python, a public IP address162MOBILE NETWORKINGand some ports open in the firewall.
This is also a typical setting in acompany or university which often has several servers accessible fromthe Internet.Environment D: Phone Internet access and an external web serverIf you are only interested in developing web-based services, a web hotelwhich supports CGI, Ruby on Rails or a web back end of your choice issufficient.
Note, however, that you cannot use the example servers in thischapter without adapting them to use your web back end.8.2.4Testing the ConnectionIn this section, we test that your network environment is correctly set upfor mobile development. According to your network environment, followinstructions in one of the sections below. Except in environment D, wefirst have to find out the server’s IP address and then test the connectionusing a simple test server.Environments A, B and C require a working Python installation on yourserver. You can find Python for Windows, Linux and Mac OS X at thePython home page at www.python.org.Environment A: Local wireless networkFirst you have to find out your IP address in your local network.In Linux and Mac OS X you can use the following Python script to findout all IP addresses that have been assigned to your computer.
You mayhave several of them, for instance, if you have both wired and wirelessnetwork adapters on your laptop. Make sure that you have joined yourlocal wireless network before executing the following script:import os, retxt = os.popen("/sbin/ifconfig").read()for t, ip in re.findall("inet (addr:)?([\d\.]+)", txt):print ipSave the code above to the file findip.py and execute it on thecommand line using the command python findip.py. Do not worryabout the code of this script.In Windows, you have to execute command ipconfig in yourcommand shell to find out the addresses.If you see an address starting with ‘192.’, it is probably your local IPaddress. Special address ‘127.0.0.1 is used only for internal communication by your computer and it is not the one we are interested in.