×

Discussion Board

Results 1 to 13 of 13
  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    17

    Proof of concept: location aware web with Python!

    I don't know if this has been demonstrated already, but zip file below shows how to let specific web sites access phone features by connecting to special-purpose web server on the phone. The code is intentionally very skimpy on features, as it just demonstrates how this works in practice.

    Basically this code allows your AJAX-style web page (and potentially your server, too) to "see" last observed GPS coordinates of your phone. One can make all kinds of nifty experiments with this. I've managed to implement a bus route optimizer on top of it, for instance. All it required was writing web server CGI scripts and HTML pages with Javascript. The "bus optimizer" can be asked to provide a bus route from "here" "home", for instance.

    Please read the README.txt file first. As this piece of code requires signing with devcert to get gain required capabilities and needs minor customisation to work specifically with your web site, it's not prepackaged for easy installation.

    The link: http://www.kirma.fi/gps-example.zip

    The code for the impatient:

    gpsd.py, the GPS-HTTP server on the phone:
    Code:
    #!/usr/bin/env python
    
    # How to expose S60 APIs to web sites shown on the browser.
    # In this case, simply make GPS coordinates visible on the site.
    # This file is intended to be used with gps-example.html
    
    # 2008 (c) Jari Kirma <kirma@cs.hut.fi>
    
    import socket
    import positioning
    
    # XXX Adjust this to match your domain
    mydomain = "example.com"
    
    lat = lon = False
    
    def position_callback(event):
    	global lat, lon
    
    	# Just keep the last observed position.
    	lat = event['position']['latitude']
    	lon = event['position']['longitude']
    
    positioning.set_requestors([{'type': 'service', 'format': 'application', 'data': 'test_app'}])
    positioning.position(callback=position_callback)
    
    serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    serversocket.bind(('127.0.0.1', 80))
    serversocket.listen(2)
    
    while True:
            (clientsocket, address) = serversocket.accept()
    
    	s = clientsocket.makefile()
    
    	# Extremely dumb HTTP server, just read input and spew the answer back
    	req = s.readline()
    
    	while True:
    		header = s.readline()
    
    		if header == '\r\n':
    			break
    
    	lats = lons = 'false'
    
    	if lat and lon:
    		lats = str(lat)
    		lons = str(lon)
    		
    	# HTTP response, call parent.data_handler with related data
    	s.write('HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\nConnection: close\r\nContent-type: text/html\r\n\r\n<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"><HTML><HEAD><TITLE></TITLE></HEAD><BODY><SCRIPT type="text/javascript">\n/* <![CDATA[ */\ndocument.domain="' + mydomain + '";parent.data_handler({"lat":' + lats + ',"lon":' + lons + '})\n/* ]]> */\n</SCRIPT></BODY></HTML>')
    
    	s.close()
    	clientsocket.close()
    gps-example.html, the web page on your web site:
    Code:
    <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
    <HTML>
    <HEAD>
    <STYLE type='text/css'>
    IFRAME { width: 0px; height: 0px }
    </STYLE>
    <SCRIPT type='text/javascript'>
    /* <![CDATA[ */
    
    // How to expose S60 APIs to web sites shown on the browser.
    // In this case, simply make GPS coordinates visible on the site.
    // This file is intended to be used with gpsd.py
    
    // 2008 (c) Jari Kirma <kirma@cs.hut.fi>
    
    // XXX Adjust this to match your domain
    mydomain = "example.com"
    // XXX and this to a host name giving a loopback address (127.0.0.1) from your DNS server
    // XXX in this case, localhost.example.com == 127.0.0.1
    localserver = "localhost"
    
    document.domain = mydomain
    
    function data_handler(data) {
      window.clearTimeout(timeout)
      document.getElementById("info").innerHTML = "Location coordinates: " + data.lat + ", " + data.lon
    
      // Make it repeat
      window.setTimeout(onload_body, 5000)
    }
    
    function noserver_tell() {
      document.getElementById("info").innerHTML = "Response timeout, no server running?"
    }
    
    function onload_body() {
      timeout = window.setTimeout(noserver_tell, 5000)
    
      document.getElementById("gps").src = "http://" + localserver + "." + mydomain + "/?nonce=" + new Date().getTime()
    }
    
    /* ]]> */
    </SCRIPT>
    <TITLE>GPS-aware web on S60</TITLE>
    </HEAD>
    <BODY bgcolor='#ffffff' onload='onload_body()'>
    <DIV id='info'>Connecting to the server...</DIV>
    <IFRAME id='gps' src='' frameborder=0></IFRAME>
    </BODY>
    </HTML>
    Last edited by jkirma; 2008-01-08 at 07:29. Reason: Added code.

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    17

    Re: Proof of concept: location aware web with Python!

    Considering reasonable amount of downloads and views on the forum but no replies, I'm wondering about these points:

    Has anyone experimented with my code? Did it work? (It certainly works for me...)

    Has anyone done something similar? That is, making a web site more aware of phone capabilities by implementing a "stub server" providing the data from the phone to the web browser running on it, affecting data being transferred with the server in the end? Althought they're nifty, I don't consider applications starting the browser with context-dependent URL exactly the same as just having autostarted server providing the data to your site when you surf to it with the phone browser...

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    568

    Re: Proof of concept: location aware web with Python!

    Hi Jkirma,

    the general laziness of this forum is known and shouldn't aggrieve you too much One shouldn't expect to get flooded with answers and/or responses here...

    I didn't try out your code, but from inspecting it I see:

    1) You are running a webserver on your phone
    2) You are using internal S60 APIs and exposing the results on behalf of a querying website.

    Hmm. Interesting. Can you tell me more about the application you have in mind?

    Regards

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    17

    Re: Proof of concept: location aware web with Python!

    Quote Originally Posted by neil.young View Post
    Hmm. Interesting. Can you tell me more about the application you have in mind?
    The most useful functions, at least for me, are related to location awareness on web sites, like showing bus routes based on GPS coordinates and/or Cell ID information, geotagging blog comments and similar entries, and so on. On top of my proof of concept it was fairly easy to write the location aware wrapper for bus route optimizer here in Helsinki.

    Access to other kinds of phone functions might be possible, although I haven't given too much thought for this. Setting alarms and adding contacts and meetings automatically are some possibilities, and one could make this work in reverse too, making the web page (and the web site server outside the phone) aware of this data on the phone, for instance making a web form aware of the contacts on the phone and being able to move some of those to the site.

    Of course, there are security implications that need to be addressed properly by the phone web server writer. Once these are addressed, adding awareness of the phone functionalities to a web site may be rather straightforward thing for the web site developer... of course, the phone web server must be provided for the users.

    These kind of things are getting at least de facto standardisation sooner or later, maybe directly in the browser, but running a separate web server with these tricks is a good way to kickstart prototyping this kind of functionality. And the whole point of web development in this case is to avoid reimplementing applications for multiple platforms when web2.0-ish web pages work on the devices well enough.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    4

    Re: Proof of concept: location aware web with Python!

    Hi.

    I've looked at the code and there is something I can't understand... The html side try to retrieve data from "127.0.0.1", and there is supposed to be listening the pys60 server..

    So, this only make sense to me if both parts are in the same machine (the mobile), like viewing the html in the local browser..

    Is this correct?

    Wouldn't be more usefull to be done remotely?

    Perhaps a good solution could be use the recently released PAMP project, and a pys60 CGI as the location server.

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    17

    Re: Proof of concept: location aware web with Python!

    Quote Originally Posted by canopus2 View Post
    Hi.

    I've looked at the code and there is something I can't understand... The html side try to retrieve data from "127.0.0.1", and there is supposed to be listening the pys60 server..

    So, this only make sense to me if both parts are in the same machine (the mobile), like viewing the html in the local browser..
    I guess you've misunderstood something. Indeed, the HTML side attempts to contact a host with an IP address of 127.0.0.1, and that's where the phone web server resides (serving content only to the clients on the phone).

    The logic behind this is that only when phone user accesses web sites there's a need for any of these services (such as GPS coordinates). Thanks to modern web browsing and its allowance of cross-site scripting, it is possible to transfer information from the phone server to the regular web server through web page Javascript when the scripts and DNS are configured properly.

    Quote Originally Posted by canopus2 View Post
    Perhaps a good solution could be use the recently released PAMP project, and a pys60 CGI as the location server.
    That certainly is an alternative. It may also be an disadvantage when you consider the fact that PAMP server maintains a TCP connection to the proxy server whenever it's active, just to punch through operator firewalls in front of the actual handset network; it consumes considerable amounts of energy. If you want to keep track of the phone at all times that makes sense, but if you care about the results only when the phone user is browsing, it's another thing altogether. Running a HTTP server in a localhost address doesn't cause radio activity; it's essentially free regarding battery use (of course tracking GPS signal can eat the battery, but that's not the same thing). So, you have a point, but PAMP is really way too expensive if you want to provide the coordinates to the user, not the people outside the phone.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    4

    Re: Proof of concept: location aware web with Python!

    Quote Originally Posted by jkirma View Post
    I guess you've misunderstood something. Indeed, the HTML side attempts to contact a host with an IP address of 127.0.0.1, and that's where the phone web server resides (serving content only to the clients on the phone).
    Yes, sorry, I was thinking on a web service showing the phone location from outside the phone...

    Now It's clear to me. The HTML page is accessed from the phone user browser. Then the scripts in the page access the local position information (python socket server) and can do whatever you want with the information obtained: show to the user, send to another service...

    The location information is used only when the user wants, and this is a lot lighter than PAMP, of course.

    Good trick!

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    10

    Re: Proof of concept: location aware web with Python!

    Hi.

    This looks to be exactly what I've been hunting for! Just an idiot query. The domain - I'm going to run this on my N95 PAMP server - so will that be the domain of e.g. mymobilesite.net or a LAN if I use Wi-Fi? Not sure what the 'phone' domain is, if that's what you mean? Help!

  9. #9
    Registered User
    Join Date
    Jul 2004
    Posts
    17

    Re: Proof of concept: location aware web with Python!

    Quote Originally Posted by maddo View Post
    Hi.

    This looks to be exactly what I've been hunting for! Just an idiot query. The domain - I'm going to run this on my N95 PAMP server - so will that be the domain of e.g. mymobilesite.net or a LAN if I use Wi-Fi? Not sure what the 'phone' domain is, if that's what you mean? Help!
    Unfortunately - based on your reference to PAMP and mymobilesite.net - I suspect my work isn't exactly what you're looking for. You might be able to loan some ideas from the HTML/Javascript part, but the server part is not practically usable in this case.

    In addition, even as a stand-alone system, my code relies on ability to add records to the DNS zone (in your case, this would be mymobilesite.net, which you unfortunately can't control) to "bring" the phone web server to the same security domain with the remote server/page using the HTTP data.

    I haven't really concentrated my efforts to PAMP, but I guess easiest way to do these kinds of things in your case would be to write a more or less complete front-end/pass-through web page/service on your PAMP server. It would acquire GPS coordinates itself, send them in a query to outside server that does the "business intelligence" and write the acquired results directly or after some processing as the reply to the original requestor (phone user, or remote mymobilesite.net user).

    Running PAMP is vastly more resource-intensive than my small demo piece, but then again, my demo piece is intended only to help phone user browsing sites in the Internet by supplying the location data to them. It can't and doesn't attempt to make this information visible to browsers in the Internet (others than user on the phone).

  10. #10
    Registered User
    Join Date
    Aug 2008
    Location
    Bergen, Norway
    Posts
    8

    Re: Proof of concept: location aware web with Python!

    Hey,
    I'm very interested in this solution as yo can see from my post what I'm trying to do http://discussion.forum.nokia.com/fo...d.php?t=149146.

    I have a newbie questions. Why is it necessary to make a sis from the py file. Isn't possible just to run the script in the python shell?

    If not, how do I make to a sis?

  11. #11
    Nokia Developer Moderator
    Join Date
    May 2007
    Location
    21.46 N 72.11 E
    Posts
    3,796

    Re: Proof of concept: location aware web with Python!

    Its not necessary to convert py script to sis However, if you are planning to make a standalone application you need to make a sis file.

    For making sis files, use ensymble for 3rd edition devices. Please check the FAQ for more details.
    Pankaj Nathani
    www.croozeus.com

  12. #12
    Registered User
    Join Date
    Aug 2008
    Location
    Bergen, Norway
    Posts
    8

    Re: Proof of concept: location aware web with Python!

    Thanks for quick answer,
    I'm trying to set this up on the localhost on my mac now. In what order do I need to do things on my mobile?

    First run the python script, then open the webrowser and direct it to my localhost adress "http://example.localhost./Name/gps-example.html" ?

    So far I'm only getting the timeout "Response timeout, no server running?"

  13. #13
    Regular Contributor
    Join Date
    Jan 2004
    Location
    Helsinki
    Posts
    376

    Cool Re: Proof of concept: location aware web with Python!

    There is something little more out there:

    http://lifematta.com

    Not so proof of concept anymore
    Mikko Ohtamaa

    http://mfabrik.com
    http://blog.mfabrik.com

Similar Threads

  1. Network based location in Python?
    By miohtama in forum Symbian
    Replies: 4
    Last Post: 2007-12-31, 09:40
  2. Is there web services support for python on S60
    By skarthiks in forum Symbian
    Replies: 0
    Last Post: 2007-06-08, 23:35
  3. Series 60 Concept Emulator (SDK Beta 0.2 Linux) not working
    By mattbee in forum Mobile Java Tools & SDKs
    Replies: 1
    Last Post: 2003-06-10, 11:43
  4. Series 60Series 60 MIDP Concept SDK Beta 0.2 Linux bug?
    By kauppi in forum Mobile Java Tools & SDKs
    Replies: 3
    Last Post: 2003-04-07, 09:05

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×