×
Namespaces

Variants
Actions

Archived:Controlling a device remotely using PySymbian

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngAquivado: Este artigo foi arquivado, pois o conteúdo não é mais considerado relevante para se criar soluções comerciais atuais. Se você achar que este artigo ainda é importante, inclua o template {{ForArchiveReview|escreva a sua justificativa}}.

All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.

Article Metadata
Code Example
Source file: Media:PHPRemote.zip
Tested with
Devices(s): 5800XM
Compatibility
Platform(s): S60 5th Edition
Article
Keywords: PHPRemote, urllib, PHP, messaging, inbox
Created: User:Chris.jeganathan (30 Jul 2010)
Last edited: hamishwillee (31 May 2013)

Original author: Chris Jeganathan
This article provides an overview of the PHPRemote example application: a Python based remote control for a mobile device that can be used to retrieve the device message inbox and general information, or to send SMS messages. The article includes a brief discussion of the NetworkFunctions class that shows how how to use the urllib and httplib modules.

Note that the demonstration is primarily intended to show how easy the Python networking functions are to learn and use; As discussed in the "Next steps" section, this is not necessarily the best way to implement device remote control functionality!

Comes with Code: File:PHPRemote.zip

Contents

Introduction

The PHPRemote example consists of 3 parts; a Python application running on Windows, a Python on Symbian client application running on the device and a simple HTTP server (written in PHP) which they use to communicate.

The PC "sender" application and device "receiver" applications connect using a simple protocol:

  1. The sender connects to the server via http and uses the server API to specify the action/information required.
  2. The server writes the command to a file, setting a value in the file to zero (_net.page[4] = 0).
  3. The sender then starts polling the server (regularly reads the file) to see if the _net.page[4] value has changed to "1", indicating a response from the device.
  4. The receiver is meanwhile connected to the server using HTTP and polling the file to see whether there is a message from the sender (i.e. that the value of _net.page[4] is zero).
  5. If there is a message the receiver executes the command and then responds by calling the server API. This sets the value of _net.page[4] to "1".
  6. Finally, the "sender" application downloads the response from the server and presents it to the user.

At no point is there a direct connection between the applications. As a result, if one of the clients is turned off, the other can still complete the command.

PC-based send application
Python on Symbian receive application

Setting up the code

Warning.pngWarning: Data sent using this example code is highly insecure.

The example code can be downloaded from: File:PHPRemote.zip

In order to run the example you will need online hosting space that allows PHP, which you can access using FTP. On this site you will need the server script settxt.php. You should also create a blank text file in the same locaion with the name PySymbianserver.txt. Both of these files will need full read/write permissions (777) which can easily be edited on most FTP clients. The PHP script is simple and self explanatory, and should not require any change.

The Python scripts will both need to be edited slightly to set the location of the PHP script. For example, consider the case where your server files are at the root of http://www.examplesite.com:

  -www.examplesite.com
|--pys60server.txt
|--settxt.php

Then the following variables must to be changed in both Python files from:

WEB_SERVER = ""
TEXT_FILE = ""
PHP_FILE = ""

To:

WEB_SERVER = "www.examplesite.com"
TEXT_FILE = "/pys60server.txt"
PHP_FILE = "/settxt.php"

After changing the values above the PySymbian script can be run on the the mobile device and the PC script can be run on the PC.

Note that the example package also contains a file called "PHPRemote - ServerWatcher.py", which is for testing purposes only.

Using the NetworkFunctions class

This section shows the Windowsversion of the NetworkFunctions class used to communicate with the server.

import urllib
import httplib
 
class NetworkFunctions:
 
'''
Attempts to download latest data from the server. This function
returns false if the connection failed and true if the connection
succeeded.
'''

def download_data(self):
 
# Attempts to download the initial data from the server
try:
url = urllib.urlopen("http://" + WEB_SERVER + TEXT_FILE)
self.page = url.readlines()
url.close()
except:
menu.listbox.insert(0, 'Please check network connection!')
menu.listbox.insert(0, 'Error reading current data from server')
return False
 
# Strips all data of the newline (\n) whitespace character in
# the text file
for x in range(0,len(self.page)):
self.page[x] = self.page[x].strip()
return True
 
'''
Attempts to upload the data with the given parameters. This function
returns false if the connection failed and true if the connection
succeeded.
'''

def upload_data(self, action, content1, content2, content3, done):
# Encodes the parameters and sets the headers
try:
params = urllib.urlencode({'action': action,
'content1': content1,
'content2': content2,
'content3': content3,
'done': done})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
 
# Creates the connection with the system and posts
conn = httplib.HTTPConnection(WEB_SERVER)
conn.request("POST", PHP_FILE, params, headers)
conn.close()
menu.listbox.insert(0,"Command Uploaded!")
return True
except:
menu.listbox.insert(0, 'Error uploading to PHP')
return False

The PySymbian version is similar - the main difference being that the tkinter windows UI listbox functions were changed to print functions. E.g. Windows:

menu.listbox.insert(0,"Hello World")

Were changed to:

print("Hello World")

Note also that this class can be extended to send other values. The changes need to be made in parallel with the PHP script as shown below:

def upload_data(self, variable1, variable2, variable3, variable4, variable5):
params = urllib.urlencode({'variable1': 1,
'variable2': 2,
'variable3': 3,
'variable4': 4,
'variable5': 5})

Would require this associated code in the PHP script:

$variable1 = $_POST['variable1'];
$variable2 = $_POST['variable2'];
$variable3 = $_POST['variable3'];
$variable4 = $_POST['variable4'];
$variable5 = $_POST['variable5'];

Next steps

This is only example code. A commercial application would need significant enhancements

  1. Giving server files full permissions and using PHP means that transmitted data is very insecure. We could adjust permissions and consider a safer connection such as FTP (ftplib).
  2. The PC application could easily be made into a mobile application by changing it to use the appuifw module instead of tkinter.
  3. A more complicated protocol could be used to allow the batching of messages
  4. The server API could be improved so that direct access to the pys60server.txt is not required.


Licence icon cc-by-sa 3.0-88x31.png© 2010 Symbian Foundation Limited. This document is licensed under the Creative Commons Attribution-Share Alike 2.0 license. See http://creativecommons.org/licenses/by-sa/2.0/legalcode for the full terms of the license.
Note that this content was originally hosted on the Symbian Foundation developer wiki.

This page was last modified on 31 May 2013, at 04:03.
60 page views in the last 30 days.
×