labrad / pylabrad

python interface for labrad
51 stars 31 forks source link

How to create a server and connect it to another server? #347

Closed Xie-Tian closed 7 years ago

Xie-Tian commented 7 years ago

Hi, I ran into a problem: I need to construct a server using labrad, and meanwhile, treat it as a client, connecting it to another server. Here's my easy code.


from labrad.server import LabradServer, setting
import labrad

SERVERNAME = 'tian'

cxn = labrad.connect('192.xxx.xxx.xx', password='xxxxxxxxx', tls_mode='off')

class Tian_Server(LabradServer):

    name = SERVERNAME

    def initServer(self):
        pass

if __name__ == "__main__":
    from labrad import util
    util.runServer( Tian_Server() )
```python

But it seems that there is a twisted.internet.error.
maffoo commented 7 years ago

There's no need to call labrad.connect when running a server. Instead you should pass the configuration such as the manager host and password as command line arguments, or set them in environment variables. When the server starts it will connect to the labrad manager on its own using that configuration.

Xie-Tian commented 7 years ago

Thanks for your quick reply! If I want to set them in environment variables, is in this way:?

from labrad.server import LabradServer, setting
import labrad

SERVERNAME = 'tian'

class Tian_Server(LabradServer):

    name = SERVERNAME

    def initServer(self):
        cxn = labrad.connect('192.xxx.xxx.xx', password='xxxxxxxxx', tls_mode='off')
        dac = cxn.laserdac_server
        pass

if __name__ == "__main__":
    from labrad import util
    server = Tian_Server()
    util.runServer( server )

But it raises a timeout error when initiating.

DanielSank commented 7 years ago

To run a command in bash with environment variables, do this

LABRADHOST=whatever LABRADPASSWORD=whatever python myserver.py
maffoo commented 7 years ago

Environment variables are set in your shell before launching python, for example using the export command in bash, or the set command in windows (alternately, in windows you can set environment variables in the system configuration dialogs so that they will be available if you launch servers in other ways). You can also pass command-line arguments to the program when you run it.

So, suppose you save the server in a script called tian_server.py, then you could do the following in a windows command prompt:

set LABRADHOST=192.xxx.xxx.xxx
set LABRADPASSWORD=xxxxxxxx
python tian_server.py

or, with command line arguments:

python tian_server.py --host=192.xxx.xxx.xxx --password=xxxxxxx

In the server itself, as I said, there is no need to call labrad.connect. The server will connect to the labrad manager and you can just reuse the connection that it has, which is available as self.client.

Xie-Tian commented 7 years ago

Thanks for kind instructions. I do the following in a windows command prompt: set LABRADHOST=192.xxx.xxx.xxx set LABRADPASSWORD=xxxxxxxx python tian_server.py

It now could run. But how could I use it by self.client? I try in this way to connect:

from labrad.server import LabradServer, setting
import labrad

SERVERNAME = 'tian'

class Tian_Server(LabradServer):

    name = SERVERNAME

    def initServer(self):

        pass

    @setting(1, "test")
    def test(self,c):
        cxn = yield self.client
        dac = cxn.laserdac_server

if __name__ == "__main__":
    from labrad import util
    server = Tian_Server()
    util.runServer( server )

I want use dac, but I shows that: dac = cxn.laserdac_server exceptions.AttributeError: 'ClientAsync' object has no attribute 'laserdac_server' [payload=None]

Thanks a lot!