Omnis Studio and Python

serban21 at gmail.com serban21 at gmail.com
Tue Dec 23 06:00:37 EST 2014


Hi

In line "resp = requests.post(url=URL,data=request,
auth=(USERNAME,PASSORD))" request can be several things, a dictionary, a
string or a file.

Before going ahead, it's a bad idea to use "request" as a variable name in
this context, since it's just a 's' away from the module name. It's like
having a local variable in Studio with the same name as an object class. It
works, but it's confusing. I'll use data instead.

The data parameter is "Dictionary, bytes, or file-like object to send in
the body of the Request" (quote from
http://docs.python-requests.org/en/latest/api/#main-interface). So you
could just put an XML string in there, if the server accepts it. If you put
a dictionary, that will be form-encoded (see
http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests).
If you need to send files (and the server accepts it) you should use the
files argument (quote from docs "files – (optional) Dictionary of 'name':
file-like-objects (or {'name': ('filename', fileobj)}) for multipart
encoding upload")

You don't have to put the URL parameters in it, you can also put them like
this:

import requests
import sys

params = {
    'OmnisLibrary': 'MyHttp',
    'OmnisClass': 'rtHttpDoTest',
    'data': sys.argv[1]
}
if USERNAME is None:
    auth = None
else:
    auth = (USERNAME, PASSWORD)

resp = requests.post(url=URL, params=params, auth=auth)
print resp.text

This will do the same as Greg's code. It's not complete, it assumes that
the URL, USERNAME and PASSWORDS variables are somehow set, and will read
the data you want to send from the command line argument, instead of a text
file.

The auth parameter is a pair of username and password values for basic HTTP
authentication; in normal ultra thin Studio you'll not need that. For
details about that, see
http://docs.python-requests.org/en/latest/user/advanced/#custom-authentication
and http://en.wikipedia.org/wiki/Basic_access_authentication.

On Lou question, I've been working with Omnis (O7, O$) for 16 years, went
to a couple of EurOmnises, and switched to Python about an year ago. Python
pays better, there is a library for everything in Python (unlike Omnis...)
and it's a similar language in some ways. So no chance of participating
anymore in EurOmnis, just lurking here on the list...

Regards,
Serban Teodorescu




2014-12-23 3:39 GMT+02:00 Grzegorz (Greg) Pasternak <grzegorz at niagara.com>:

> Lou;
>
> > Serban's point on data=[array] first, plus requisite authentication
> (Basic Auth?), is spot on.
>
> What are you suggesting?
>
> I have posted in my previous email the script that works.  However,…
> - assuming that I would like to keep in script this line:
>
>         resp = requests.post(url=URL,data=request, auth=(USERNAME,PASSORD))
>
> How do I trap the "data stuff" in Omnis?
> Let's assume that "request" is simply XML formatted string.
>
> What would be the purpose of "auth=(USERNAME,PASSORD)" since I only want
> to run method in the library?
> (there is no database connection needed, just run test method in the
> instantiated object class)
>
> Can you elaborate on this?
> I am sure this may be helpful for not just for me.
>
> Thanks,
> Greg
>
>
> On Dec 22, 2014, at 7:15 PM, Lou Picciano <loupicciano at comcast.net> wrote:
>
> > Serban, Derek, Greg et al,
> >
> > Funny you guys happen to be on this, today.
> >
> > Couple of thoughts:
> > 1) +++ on Serban's point: requests is the New Way Forward here. Use it.
> httplib likely the also-ran at this point...
> > 2) Serban, who are you - and why are you not at EurOmnis?
> > 3) Derek, glad to have you on this Train of Thought.
> >
> > Serban's point on data=[array] first, plus requisite authentication
> (Basic Auth?), is spot on.
> >
> > Lou Picciano*
> >
> > *(hesitant to call himself 'experienced' in this, has spent the last few
> days trying to get the whole lxml/etree/(etc) foodchain built - for use by
> one of the Omnis Luminaries - with static dependencies, of course, on
> multicore UNIX, using the Solaris linker. Head spinning.)
> >
> > ----- Original Message -----
> >
> > From: serban21 at gmail.com
> > To: "OmnisDev List - English" <omnisdev-en at lists.omnis-dev.com>
> > Sent: Monday, December 22, 2014 1:05:41 PM
> > Subject: Re: Omnis Studio and Python
> >
> > Hi
> >
> > I'm working as a Python developer now, so I should be able to help...
> >
> > The additional data should be passed in the request variable (in
> > data=request). That is supposed to be a dictionary, and it should be
> > populated before that call. Do you have that code?
> >
> > See
> >
> http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests
> > .
> >
> > Derek example is ok, but it's using another library than yours (httplib
> vs
> > requests). For this purpose, requests is much better.
> >
> >
> > Regards,
> > Serban Teodorescu
> >
> >
> > 2014-12-22 19:53 GMT+02:00 Derek Delpero <derek.delpero at gmail.com>:
> >
> >> Hi,
> >>
> >> Here's an example using a get request:
> >>
> >> import httplib
> >> conn = httplib.HTTPConnection("172.16.25.133:5921")
> >>
> >>
> conn.request("GET","/Ultra?OmnisLibrary=TestLibrary&OmnisClass=TestRemoteTask&test_parameter=1")
> >> res = conn.getresponse()
> >> data = res.read()
> >> conn.close()
> >>
> >> Hope that helps,
> >>
> >> Derek
> >>
> >>
> >> On Mon, Dec 22, 2014 at 10:54 AM, Grzegorz (Greg) Pasternak <
> >> grzegorz at niagara.com> wrote:
> >>>
> >>> Hello;
> >>> I am looking for some help re: Omnis Studio 4.x and up with Python.
> >>> Specifically I would like to run Python script against Omnis library
> >> using
> >>> HTTP ultra approach.
> >>> In archives I found reference to
> >>> Integrating Python With Omnis
> >>> http://omniscentral.com/integrating-python-with-omnis
> >>> but I can't seem to get more information.
> >>> I think this is the line in Python script that needs additional info
> >> about
> >>> Omnis library and Remote task class name:
> >>>
> >>> resp = requests.post(url=URL,data=request, auth=(USERNAME,PASSORD))
> >>>
> >>> Is anybody out there who has some experience with this and can help?
> >>> Please let me know,
> >>> Regards;
> >>> Greg
> >>>
> >>> _____________________________________________________________
> >>> Manage your list subscriptions at http://lists.omnis-dev.com
> >>>
> >> _____________________________________________________________
> >> Manage your list subscriptions at http://lists.omnis-dev.com
> >>
> > _____________________________________________________________
> > Manage your list subscriptions at http://lists.omnis-dev.com
> >
> > _____________________________________________________________
> > Manage your list subscriptions at http://lists.omnis-dev.com
>
> _____________________________________________________________
> Manage your list subscriptions at http://lists.omnis-dev.com
>



More information about the omnisdev-en mailing list