I am trying to use gupnp
(GNU library for UPnP and DLNA operations), using GObjectIntrospection:
import functools
import gi
gi.require_version( 'GSSDP', '1.0' )
from gi.repository import GSSDP
from gi.repository import Gtk
__callback = None
__network = None
__msgType = None
__client = None
__browser = None
def start( cb, network = '0.0.0.0', msgtype = 'ssdp:all' ):
global __client, __browser, __callback
# Si ya hay un descubrimiento en marcha, no hacemos nada.
if __client != None:
if cb == __callback:
return
else:
raise( 'Ya hay un descubrimiento en marcha' )
__callback = cb
__client = GSSDP.Client.new( )
__client.set_network( network )
__browser = GSSDP.ResourceBrowser.new( __client, msgtype )
__browser.connect( 'resource-available', __available )
__browser.connect( 'resource-unavailable', __unavailable )
__browser.set_active( True )
def stop( ):
__browser.set_active( False )
def __available( notUsed, msn, locations ):
__callback( msn, locations )
def __unavailable( notUsed, msn ):
__callback( msn, None )
def debug( msn, locations ):
if locations == None:
print( 'PERDIDO', msn )
else:
print( 'ENCONTRADO', msn )
for v in locations:
print( v )
start( debug )
Gtk.main( )
To work, it works, at least to discover UPnP devices:
FOUND uuid: 20809696-105a-3721-e8b8-c4a366da3c13 :: upnp: rootdevice link
FOUND uuid: 20809696-105a-3721-e8b8-c4a366da3c13 link
...
The funny thing happens when I try to stop it, by pressing Ctrl + C :
Traceback (most recent call last):
File "gssdp.py", line 38, in __unavailable
NameError: name '__callback' is not defined
How? O_o But I'm using __callback
everywhere!
The complete message is:
^ CTraceback (most recent call last):
File "gssdp.py", line 49, in < module & gtk.main ()
File "/usr/lib/python3/dist-packages/gi/overrides/Gtk.py", line 1629, in main
return _Gtk_main (* args, ** kwargs)
File "/usr/lib/python3.6/contextlib.py", line 88, in exit
next (self.gen)
File "/usr/lib/python3/dist-packages/gi/_ossighelper.py", line 251, in register_sigint_fallback
signal.default_int_handler (signal.SIGINT, None)
KeyboardInterrupt Traceback (most recent call last):
File "gssdp.py", line 38, in __unavailable
NameError: name '__callback' is not defined
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 53, in apport_excepthook
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 24, in enabled
ImportError: sys.meta_path is None, Python is likely shutting down
The code is not intended to be used independently, but is part of a Gtk application. When I use it from the real application, with a window on screen, the same curious exception occurs when closing it, doing click in the corresponding icon of its title bar.
Note : I'm sure it's not a typo ; I have copied / pasted __callback
, and it continues to be produced.
EDITO
I made some small changes:
start( debug )
GObject.timeout_add( 2000, stop )
GObject.timeout_add( 2500, Gtk.main_quit )
Gtk.main( )
This way, I stop the discovery at 2 seconds, and I finish the application at 2.5 seconds.
In doing so, the exception does not occur .