Pulling LabKey data via python api

LabKey Support Forum (Inactive)
Pulling LabKey data via python api james skane 93089  2016-07-15 11:14
Status: Closed
 
After setting up my netrc file according to the instructions, I have not been able to pull any data from labkey. Instead am receiving a "ServerContextError" when using the python export script that labkey provides. Is there anything wrong with the following script?


import labkey

server_context = labkey.utils.create_server_context('52.36.10.100', 'home/db_test', use_ssl=True)

my_results = labkey.query.select_rows(
    server_context=server_context,
    schema_name='study',
    query_name='db_test'
)


The ServerContextError is somewhat vague, so any advice on how to resolve this issue would be greatly appreciated!
 
 
Jon (LabKey DevOps) responded:  2016-07-18 19:51
Hi James,

Have you checked our docs here? https://github.com/LabKey/labkey-api-python

Your script looks semi-complete since I'm seeing "import labkey" instead of:

from labkey.utils import create_server_context
from labkey.query import select_rows

Mind you, this is assuming you're using the latest version of our Python API. Do you know what version of the Python API you're using?

Also, is there a query or table called "db_test"? I checked the IP address and from what I'm seeing, you just have a table called "Database":

https://52.36.10.100/study/home/db_test/dataset.view?datasetId=5001

Regards,

Jon
 
james skane 93089 responded:  2016-07-18 21:59
Hi Jon,

Thanks for your response. db_test was a table that was made earlier during our attempts to pull data with labkey. I am using version 0.4.2. After changing the imports above and the table name I am still receiving the same ServerContextError. Here is the script below:


from labkey.query import select_rows
from labkey.utils import create_server_context

server_context = create_server_context('52.36.10.100', 'home/db_test', use_ssl=True)

my_results = select_rows(
    server_context=server_context,
    schema_name='study',
    query_name='Database'
)

And below is the error:

Traceback (most recent call last):
  File "/Users/...", line 11, in <module>
    query_name='Database'
  File "/Users/...", line 274, in select_rows
    select_rows_response = _make_request(server_context, url, payload, timeout=timeout)
  File "/Users/...", line 310, in _make_request
    raise ServerContextError(e)
labkey.exceptions.ServerContextError

Is there anything I am missing that might be causing this error?

Sincerely,
James
 
Brian Connolly responded:  2016-07-19 09:12
James,

The problem is with your SSL certificate. You can see this by catching the error from your select_rows call. For example, if you change your script to


from labkey.query import select_rows
from labkey.utils import create_server_context

server_context = create_server_context('52.36.10.100', 'home/db_test', use_ssl=True)

try:
    my_results = select_rows(
        server_context=server_context,
        schema_name='study',
        query_name='Database'
    )
except labkey.exceptions.ServerContextError as e:
    print "Exception Code: " + str(e.exception)
    print "Error Message: " + str(e.message)


You will see the error messages

  Exception Code: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)
  Error Message: Failed to match server SSL configuration. Ensure the server_context is configured correctly.


You are running into two SSL problems. The first is that the Common Name in the SSL certificate does not match the hostname you are using in the create_server_context call (in your example code, you are using the IP address of 52.36.10.100, which does not match the Common Name of your SSL certificate which is "William Bosl"). The second is that the SSL certificate is self-signed.

If you fix both of these problem with the SSL certificate then I believe the Python api will work correctly.