pythonsocketspys60

Non blocking python sockets


I'd like to write a small Bluetooth server application to my Nokia phone in PyS60. It needs to be able to send response to the client's request and be able to push data to the client as well.

option 1: if I use socket.recv(1024), the program waits until something is received, therefore the server can't push data to the client. The Python for S60 implementation is missing the socket.settimeout() method, so I couldn't write a proper non-blocking code.

oprion 2: The socket.makefile() approach was looking good, but couldn't make it work. When I replaced the conn.recv(1024) to fd = socket.makefile() fd.readline(), it didn't read a thing.

option 3: Looked into the select() function, but had no luck with it. When I changed the conn.recv() to the r,w,e = select.select([conn],[],[]) like it's been suggested the client doesn't even connect. It hangs at "Waiting for the client...". Strange...

I know that there are pretty nice server implementations and asynchronous API-s as well, but I only need a really basic stuff here. Thanks in advance!

here's what I have:

sock = btsocket.socket(btsocket.AF_BT, btsocket.SOCK_STREAM)
channel = btsocket.bt_rfcomm_get_available_server_channel(sock)
sock.bind(("", channel))                                     
sock.listen(1)
btsocket.bt_advertise_service(u"name", sock, True, btsocket.RFCOMM)

print "Waiting for the client..."                                     
conn, client_mac = sock.accept()
print "connected: " + client_mac

while True:
    try:
        data = conn.recv(1024)
        if len(data) != 0:
           print "received [%s]" % data
           if data.startswith("something"): conn.send("something\r\n")
        else:
           conn.send("some other data \r\n")
    except:
           pass

It's obviously blocking, so the "some other data" is never sent, but it's the best I've got so far. At least I can send something in reply to the client.


Solution

  • Found the solution finally!

    The select function wasn't working with the btsocket module of the newer PyS60 ports. Someone wrote a new_btsocket (available here) with a working select function.