The socket Module

(This Website is created and managed by Pankaj Nathani) 

Home   Installation   Phase 1    Phase2   Phase4   Module-Wise Examples    Modules 

 Extensions    C++Py60 Extensions    Forum Nokia(Python)    Google Group    Contact Me   

Socket Module examples

Transfer Text from One Mobile to Other Mobile

(Server File)

(Should Be running on the server device when client file is to be running on other device)
# this file is the server side
# the corresponding client side file is called rfcomm_client.py

from socket import *
import appuifw

server_socket = socket(AF_BT, SOCK_STREAM)
p = bt_rfcomm_get_available_server_channel(server_socket)
server_socket.bind(("", p))
print "bind done"
server_socket.listen(1)
bt_advertise_service( u"port", server_socket, True, RFCOMM)
set_security(server_socket, AUTH)
print "I am listening"

# Note: Don't call .send or .recv on the server_socket!
# Use the sock object returned by server_socket.accept().
(sock,peer_addr) = server_socket.accept()
print "Connection from %s"%peer_addr
test = appuifw.query(u"Type words", "text", u"")
sock.send(test+'\n')
print "sending done"
import e32
# Allow time for data to be sent to work around a bug in the socket
# module.
e32.ao_sleep(1)
sock.close()
 

(Client File)

# this file is the client side
# the corresponding server side file is called rfcomm_server.py

import socket
import appuifw
import e32

class BTReader:
 def connect(self):
 self.sock=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
 addr,services=socket.bt_discover()
 print "Discovered: %s, %s"%(addr,services)
 if len(services)>0:
 import appuifw
 choices=services.keys()
 choices.sort()
 choice=appuifw.popup_menu([unicode(services[x])+": "+x
 for x in choices],u'Choose port:')
 port=services[choices[choice]]
 else:
 port=services[services.keys()[0]]
 address=(addr,port)
 print "Connecting to "+str(address)+"...",
 self.sock.connect(address)
 print "Connected..." 
 def readline(self):
 line=[]
 while 1:
 ch=self.sock.recv(1)
 if(ch=='\n'):
 break
 line.append(ch)
 return ''.join(line)
 def close(self):
 self.sock.close()

bt=BTReader()
bt.connect()
print "Received Result: "+bt.readline()
bt.close()

Transfer File through OBEX

 (Server File)

# This is the server, the corresponding client is obex_client.py
from socket import *
import appuifw

# Create a bluetooth socket in waiting state to be connected to
s = socket(AF_BT, SOCK_STREAM)
port = bt_rfcomm_get_available_server_channel(s)
print "Binding service to port %s"%port
s.bind(("", port))
print "Service bound."

# Advertise the OBEX service, so it can be seen by other phones
service_name=u"Test OBEX service"

print "Advertising service as %s"%repr(service_name)
bt_advertise_service(service_name, s, True, OBEX)

try: 
 print "Setting security to AUTH."
 set_security(s, AUTH)

 receive_path = u"c:\\obex.txt"
 print "Receiving file."
 bt_obex_receive(s, receive_path)
 print "File received."

 import e32
 e32.ao_sleep(1)
finally:
 print "Stopping service advertising."
 bt_advertise_service(service_name, s, False, OBEX)

print "Closing socket."
s.close()
print "Socket closed."
print "Finished."
 
 

(Client File)

# the corresponding server side file is called obex_server.py

from socket import *
import appuifw
import e32

# JL: you don't need a socket for this!
## create socket
#s=socket(AF_BT,SOCK_STREAM)

# scan for other phones offering OBEX service
addr,services=bt_obex_discover()
print "Discovered: %s, %s"%(addr,services)
if len(services)>0:
 choices=services.keys()
 choices.sort()
 choice=appuifw.popup_menu([unicode(services[x])+": "+x
 for x in choices],u'Choose port:')
 port=services[choices[choice]]
else:
 port=services[services.keys()[0]]
address=(addr,port)

# create file to be sent
send_path = u"c:\\test.txt"
f=open(send_path, 'w')
f.write("hello")
f.close() # NOTE: parens were missing here before!

# send file via OBEX
print "Sending file %s to host %s port %s"%(send_path, address[0], address[1])
bt_obex_send_file(address[0], address[1], send_path)
print "File sent."

Send File To PC from Mobile

 
import appuifw
import socket
import e32
 
 
def bt_connect():
    global s
    s=socket.socket(socket.AF_BT,socket.SOCK_STREAM)
    target=''
    if not target:
        address,services=socket.bt_discover()
        print "Discovered: %s, %s"%(address,services)
        if len(services)>1:
            import appuifw
            choices=services.keys()
            choices.sort()
            choice=appuifw.popup_menu([unicode(services[x])+": "+x
                                        for x in choices],u'Choose port:')
            target=(address,services[choices[choice]])
        else:
            target=(address,services.values()[0])
    print "Connecting to "+str(target)
    s.connect(target)
    print "OK."
 
    bt_typetext()
        
 
def bt_typetext():
    global s
    test = appuifw.query(u"Type words", "text", u"")
    if test == None:
        exit_key_handler()
    else:
        s.send(test)
        bt_typetext()
 
def exit_key_handler():
    script_lock.signal()
    appuifw.app.set_exit()
 
appuifw.app.title = u"bt mob to PC"
 
script_lock = e32.Ao_lock()
 
appuifw.app.exit_key_handler = exit_key_handler()
 
bt_connect()
 
script_lock.wait()