Before presenting some server code it is important to discuss about access points selection. When some socket calls like connect() are executed, a dialog is presented, allowing users to choose which connection they want to use. To avoid further connection dialogs or even for specifying exactly where you want to put your server, it is interesting to set your access point.
Python for S60 has four special functions for dealing with this issue in the socket module (Python 1.4.x) or btsocket module (Python 1.9.x). From Pyhton for S60 documentation:
- select_access_point(): opens a popup selection where access points are listed and can be selected. Returns selected access point id.
- access_point(apid): creates access point object by given apid. Returns access point object.
- set_default_access_point(ap): sets the default access point that is used when socket is opened. Setting apo to “None” will clear default access point.
- access_points(): lists access points id’s and names that are available.
The basic idea is to create an access point object from an access point ID. Thus, this object is used to set the default access point. For instance:
>>> import btsocket
>>> api=btsocket.select_access_point()
>>> print api
8
>>> apo = btsocket.access_point(api)
>>> btsocket.set_default_access_point(apo)

Defaut access point selection dialog
The access point object has three interesting methods:
>>> dir(apo)
['ip', 'start', 'stop']
With start() it is possible to activate this access point. Run it and after some seconds the phone will show the connection icon at phone home screen. If you are using DHCP, the IP will be negociated in this phase and it may be retrieved with apo.ip(). Finally, the access point connection may be interrupted with apo.stop().

Home screen showing connection icon
Nice ? But how about to create your own selection point popup using access_points() ? The following code snippet should work with Python 1.4.x and 1.9.x:
# -*- coding: utf-8 -*- import sys from appuifw import * try: # http://discussion.forum.nokia.com/forum/showthread.php?p=575213 # Try to import 'btsocket' as 'socket' (just for 1.9.x) sys.modules['socket'] = __import__('btsocket') except ImportError: pass import socket def sel_access_point(): """ Select the default access point. Return True if the selection was done or False if not """ aps = socket.access_points() if not aps: note(u"No access points available","error") return False ap_labels = map(lambda x: x['name'], aps) item = popup_menu(ap_labels,u"Access points:") if item is None: return False apo = socket.access_point(aps[item]['iapid']) socket.set_default_access_point(apo) return True
Your customized dialog:

Customized access point dialog
So, that is all ! Next post we will strike again with networking programming, using all we have learned until now. See you there !
Related posts:
- Network programming for PyS60 (VIII) Did you do your homework ? So, I would like...
- Network programming for PyS60 (VII) Everything is about “protocols” in computer networks, doesn’t it ?...
- Network programming for PyS60 (XIV) Have you already heard about Beautiful Soup ? Beautiful Soup...
- Network programming for PyS60 (XII) Until now we have used only TCP in our examples...
- Network programming for PyS60 (XIII) In our last post we talked about multicast, a special...
Related posts brought to you by Yet Another Related Posts Plugin.
April 29, 2009
Hi, I like your blog. I visit every post!! But I have a question about web servers.
Do a web server have a good performance publishing capabilities (location, camera, internal sensors,…) of a mobile phone? I test Mobile Web Server [http://mymobilesite.net/], a port of Apache to S60, but i dont know exactly how it works. Does it use a web server gateway? Or use the mobile phone web server only? I know the proyect Mobile Web Services [http://www.mobile-ws.com/index.php] that use the last one and make a framework to publish mobile capabilities. I dont test it yet.
It will be very interesting to publish a post about this questions.
Thanks and Regards!!!
I never used MWS but I think it uses a gateway at least for holding your address. But I dont know if after obtaining your address the site continues to be used as gateway or if all communications are done between the phone and its client (via webserver).
Hi,
When using the MWS request usually go through a gateway like mymobilesite.net. This is because that mobile devices are usually not directly addressible, when connected through an operators network you are usually behind a firewall and NAT meaning that the IP address is not available from the outside, the IP addresses also change frequently and mobile networks are optimized for downlink access but not uplink access. If this is the case then every request goes through the gateway.
However you can also access the IP address of the phone directly when connected through a wireless network, but usually wirless networks also use NAT at the router so that its only accessible through the IP address to other devices on the same network.
There is a little bottle neck in setting up the connection through the gateway and extablishing a connection with the device (up to around 2-5 sec max) but after that the transmission speed is dependent on the network as in other cases.
May 4, 2009
Hi,
My doubts were resolved. Thanks.
Other questions about the theme!
Do you think a ”Local” Mobile Web Server runs well publishing mobile capabilities? I want to do an app to publish mobile capabilities (gps, camera, sensors, …) to other phone apps like web services. Is It possible with PyS60? Do you know any other app like the proyect Mobile Web Services [http://www.mobile-ws.com/index.php]?
Thanks!!!
Some capabilities will require that you sign the shell or/and your app, like GPS. But it is possible with PyS60. BTW, unfortunately I only know mobile-ws.
July 22, 2009
Hi, i tried the code above on a n82. Basically my code calls the “sel_access_point” first, before it goes into a twitter library,
However everytime the twitter api gets called, again the normal selection dialog appears…how can i rid of it asking me?
Twitter client uses socket module (via urllib, that includes it), not btsocket, where the access point selection is done.
The line
sys.modules['socket'] = __import__(’btsocket’)
forces the loading of btsocket, even when urllib imports socket module.
Try to add it.
July 23, 2009
Hi,
That doesn’t work, you will get an error due to a missing function (something along the line of _filebject _soc error) Twitter python lib uses urllib2 and not urllib?, the only way is to stick with socket only and avoid btsocket. So the “access selection” apis must be strictly socket .
But socket doesn’t have socket.access_point() function while btsocket does, so the example in the article above wont work when urllib2 was used instead.
Yes, that is the problem. I reported it at maemo some time ago.
I suggest you to use my twitter api. Extend it if necessary.
http://code.google.com/p/wordmobi/source/browse/trunk/wordmobi/src/s60twitter.py
It uses urllib and simplejson (I ported it to S60, code in the same dir).
July 24, 2009
excellent Marcelo, i will put this into my blog as well.
Don’t forget to share your blog address. I couldn’t find it.
Hot info from Maemo:
Two new APIs have been added to the _socket_ module - access_points() and set_default_access_point(). Please check the socket module documentation for more info. These APIs should be used when using standard python modules like httplib, urllib etc..
http://garage.maemo.org/tracker/?func=detail&atid=3201&aid=4347&group_id=854
September 19, 2009
does it exist a method to set socket connection timeout, to prevent application from locking in case of unavailable network connection?
Yes, there is the socket method settimeout():
from socket import *
s = socket(AF_INET,SOCK_STREAM)
s.settimeout(5) # 5s
In this case, methods using this socket will raise an exception if the operation takes more than 5s.
September 22, 2009
thanks.
looks like it is not available in 1.4.5 for 2nd edition… ![]()
is there any version for s60-2nd which supports socket timeout?
September 28, 2009
jumpjack, I don’t think so…
I don’t know if this would solve jumpjack problem, but calling method setblocking() with a param 0 would be the same as calling settimeout(0):
from socket import *
s = socket(AF_INET,SOCK_STREAM)
s.setblocking(0)
August 26, 2010
Thanks Ricardo. Does this trick work on 2nd edition phones?
Comments RSS TrackBack Identifier URI
Leave a comment











18 Comments