Changeset a316c4e
- Timestamp:
- 2007-08-20T14:50:50Z (17 years ago)
- Branches:
- master
- Children:
- ba20c39
- Parents:
- 94bd28f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
skype/skyped.py
r94bd28f ra316c4e 35 35 36 36 def sig_handler(signum, frame): 37 print 'Caught signal %d, exiting.' % signum38 37 mainloop.quit() 39 38 … … 41 40 input = fd.recv(1024) 42 41 for i in input.split("\n"): 43 if i:44 fd.send(skype.send(i.strip()) + "\n")42 if i: 43 fd.send(skype.send(i.strip()) + "\n") 45 44 return True 45 46 def server(host, port): 47 sock = socket.socket() 48 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 49 sock.bind((host, port)) 50 sock.listen(1) 51 gobject.io_add_watch(sock, gobject.IO_IN, listener) 52 53 def listener(sock, *args): 54 global conn 55 conn, addr = sock.accept() 56 fileno = conn.fileno() 57 gobject.io_add_watch(conn, gobject.IO_IN, input_handler) 58 return True 59 60 def dprint(msg): 61 if len(sys.argv) > 1 and sys.argv[1] == "-d": 62 print msg 46 63 47 64 class SkypeApi(dbus.service.Object): 48 65 def __init__(self): 49 66 bus = dbus.SessionBus() 67 try: 68 self.skype_api = bus.get_object(SKYPE_SERVICE, '/com/Skype') 69 except dbus.exceptions.DBusException: 70 sys.exit("Can't find any Skype instance. Are you sure you have started Skype?") 50 71 51 try: 52 self.skype_api = bus.get_object(SKYPE_SERVICE, '/com/Skype') 53 except dbus.exceptions.DBusException: 54 sys.exit("Can't find any Skype instance. Are you sure you have started Skype?") 72 reply = self.send('NAME ' + CLIENT_NAME) 73 if reply != 'OK': 74 sys.exit('Could not bind to Skype client') 55 75 56 reply = self.send('NAME ' + CLIENT_NAME) 57 if reply != 'OK': 58 sys.exit('Could not bind to Skype client') 59 60 reply = self.send('PROTOCOL 5') 61 dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API') 62 76 reply = self.send('PROTOCOL 5') 77 dbus.service.Object.__init__(self, bus, "/com/Skype/Client", bus_name='com.Skype.API') 63 78 64 79 # skype -> client (async) … … 67 82 global conn 68 83 text = utf8_decode(msg_text) 69 print '<<', text84 dprint('<< ' + text) 70 85 if conn: 71 86 conn.send(msg_text + "\n") … … 75 90 if not len(msg_text): 76 91 return 77 print '>> ', msg_text92 dprint('>> ' + msg_text) 78 93 try: 79 94 reply = utf8_decode(self.skype_api.Invoke(utf8_encode(msg_text))) 80 95 except dbus.exceptions.DBusException, s: 81 96 reply = str(s) 82 print '<< ', reply97 dprint('<< ' + reply) 83 98 return reply 84 99 85 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)86 skype = SkypeApi()87 signal.signal(signal.SIGINT, sig_handler)88 89 mainloop = gobject.MainLoop()90 91 def server(host, port):92 '''Initialize server and start listening.'''93 sock = socket.socket()94 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)95 sock.bind((host, port))96 sock.listen(1)97 gobject.io_add_watch(sock, gobject.IO_IN, listener)98 def listener(sock, *args):99 '''Asynchronous connection listener. Starts a handler for each connection.'''100 global conn101 conn, addr = sock.accept()102 fileno = conn.fileno()103 gobject.io_add_watch(conn, gobject.IO_IN, input_handler)104 return True105 100 if __name__=='__main__': 101 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 102 skype = SkypeApi() 103 signal.signal(signal.SIGINT, sig_handler) 104 mainloop = gobject.MainLoop() 106 105 server('localhost', 2727) 107 106 mainloop.run()
Note: See TracChangeset
for help on using the changeset viewer.