Pyro - remote connection and execute command

Discussions on extending SharpCap using the built in Python scripting functionality
Post Reply
VAntico
Posts: 5
Joined: Mon Aug 21, 2017 10:26 pm

Pyro - remote connection and execute command

#1

Post by VAntico »

I'm looking for an example of remote connection to ShapCap script with Pyro.

My application is a client .net, I thought I was using Pyrolite.

Thanks,
Vito
User avatar
admin
Site Admin
Posts: 13173
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Pyro - remote connection and execute command

#2

Post by admin »

Hi Vito,

in SharpCap scripting, enter the sample Pyro4 server code:

Code: Select all

import Pyro4

@Pyro4.expose
class GreetingMaker(object):
    def get_fortune(self, name):
        return "Hello, {0}. Here is your fortune message:\n" \
               "Behold the warranty -- the bold print giveth and the fine print taketh away.".format(name)

daemon = Pyro4.Daemon()                # make a Pyro daemon
uri = daemon.register(GreetingMaker)   # register the greeting maker as a Pyro object

print("Ready. Object uri =", uri)      # print the uri so we can use it in the client later
daemon.requestLoop()                   # start the event loop of the server to wait for calls
This should show a URI to connect to - it will change every time - something like

Code: Select all

PYRO:obj_871d6f34a2fa4e5aa56a60b1655cd270@localhost:32081
Now in another python environment, enter

Code: Select all

import Pyro4
greeting_maker = Pyro4.Proxy('PYRO:obj_871d6f34a2fa4e5aa56a60b1655cd270@localhost:32081')
print(greeting_maker.get_fortune('robin'))
Obviously change the URI to the one produced by your SharpCap scripting.

I haven't tried connecting from a library like Pyrolite, but I'd suggest getting the above working correctly then attempting to connect from Pyrolite.

cheers,

Robin
User avatar
admin
Site Admin
Posts: 13173
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Pyro - remote connection and execute command

#3

Post by admin »

Just as a note, you cannot 'expose' the SharpCap scripting objects like the SharpCap application, Cameras, Controls, etc since they are strange proxies to .NET objects and incompatible with pyro. You will need to write plain python classes to make the functionality you want available and expose those.

cheers,

Robin
User avatar
oopfan
Posts: 1321
Joined: Sat Jul 08, 2017 2:37 pm
Location: New York
Contact:

Re: Pyro - remote connection and execute command

#4

Post by oopfan »

Very neat!
VAntico
Posts: 5
Joined: Mon Aug 21, 2017 10:26 pm

Re: Pyro - remote connection and execute command

#5

Post by VAntico »

Hi Robin.
Thanks for the quick support.
I was referring to the same code segment, thinking about expanding the SharpCap methods I needed ;) .

Code: Select all

import Pyro4
@Pyro4.expose

class ext_SharpCap(object):
    def get_test(self, name):
        return "Test, {0}.".format(name)
        
daemon = Pyro4.Daemon()                        # make a Pyro daemon
 #ns = Pyro4.locateNS()                             # find the name server
uri = daemon.register(ext_SharpCap)        # register the SharpCap as a Pyro object 
  #ns.register("ext.SharpCap", uri)             # register the object with a name in the name server
print("Ready. Object uri =", uri)                 # print the uri so we can use it in the client later
daemon.requestLoop()                              # Problem: Before not possible STOP or Exit from program.
I found two needs:
  • After you run the script line
    daemon.requestLoop()
    It is no longer possible to stop executing scripts, nor even close the program. You have to kill the application from system manager.
  • Every time the script restarts, the Pyro URI changes. The solution, maybe, is to use the "Pyro name server", but needs a Python context-driven application with a separate command: "python -m Pyro4.naming", as described in: http://pythonhosted.org//Pyro4/intro.ht ... ame-server.
Thanks
Vito
User avatar
admin
Site Admin
Posts: 13173
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Pyro - remote connection and execute command

#6

Post by admin »

I think you can fix the lockup problem just by running the requestLoop in a separate thread - ie

Code: Select all

import Pyro4
import thread

@Pyro4.expose
class GreetingMaker(object):
    def get_fortune(self, name):
        return "Hello, {0}. Here is your fortune message:\n" \
               "Behold the warranty -- the bold print giveth and the fine print taketh away.".format(name)

daemon = Pyro4.Daemon()                # make a Pyro daemon
uri = daemon.register(GreetingMaker)   # register the greeting maker as a Pyro object

print("Ready. Object uri =", uri)      # print the uri so we can use it in the client later

thread.start_new_thread(daemon.requestLoop, ())
The scripting runs in its own thread anyway (not the main application thread), so this shouldn't add any new cross thread problems.

cheers,

Robin
VAntico
Posts: 5
Joined: Mon Aug 21, 2017 10:26 pm

Re: Pyro - remote connection and execute command

#7

Post by VAntico »

OK thanks!
Scripts now work, and lets you exit from the program.
The URI connection works.

Obviously, every time you restart the script/program, the URI changes :)

Cheers,
Vito
User avatar
admin
Site Admin
Posts: 13173
Joined: Sat Feb 11, 2017 3:52 pm
Location: Vale of the White Horse, UK
Contact:

Re: Pyro - remote connection and execute command

#8

Post by admin »

You could always make the python open up a known TCP port for incoming requests and make that port give out the Pyro URI to the calling application, then the calling application would connect to port 9999 (or whatever), get the Pyro URI and then connect properly.

cheers,

Robin
Post Reply