Velocity Reviews > Need help running external program

Need help running external program

Rigga
Guest
Posts: n/a

 02-27-2005
Hi,

I am running the line of code below from a shell script and it works fine,
however I am at a total loss on how i can run it from within a Python
script as every option I have tried fails and it appears to be down to the
escaping of certain characters.

wget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \" | sed -n
's/.*url="$$[^"]*$$".*/\1/p'

I want to use the above code in my program by using popen2 so i can query
the results i.e.

output, input = popen2("the code here")

print output

Any help would be appreciated.

Many thanks

RiGGa

Pink
Guest
Posts: n/a

 02-27-2005
Rigga wrote:

> Hi,
>
> I am running the line of code below from a shell script and it works fine,
> however I am at a total loss on how i can run it from within a Python
> script as every option I have tried fails and it appears to be down to the
> escaping of certain characters.
>
> wget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \" | sed -n
> 's/.*url="$$[^"]*$$".*/\1/p'

If your problem is getting a python string without worrying about how to
escape the escape sequences, try:

r"""wget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \" | sed -n
's/.*url="$$[^"]*$$".*/\1/p'"""

You should be able to pass this directly to a popen() function.

Rigga
Guest
Posts: n/a

 02-27-2005
Pink wrote:

> Rigga wrote:
>
>> Hi,
>>
>> I am running the line of code below from a shell script and it works
>> fine, however I am at a total loss on how i can run it from within a
>> Python script as every option I have tried fails and it appears to be
>> down to the escaping of certain characters.
>>
>> wget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \" | sed -n
>> 's/.*url="$$[^"]*$$".*/\1/p'

> If your problem is getting a python string without worrying about how to
> escape the escape sequences, try:
>
> r"""wget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \" | sed -n
> 's/.*url="$$[^"]*$$".*/\1/p'"""
>
> You should be able to pass this directly to a popen() function.

Hi,

Thanks for replying however I have just tried that and it does not seem to
work, it doesnt return any results (i take it the r was a typo)

Thanks

RiGGa

Leif B. Kristensen
Guest
Posts: n/a

 02-27-2005
I'm using wget from Python to get extactly one line from a reports page.
I made this function that works for me:

def wgetline(exp): # see Python Cookbook p. 228
print "Getting result from server ..."
command = 'wget -q -O - \
http://www.foobar.com/report.pl\?UserID=xxx\&UserPW=xxx \
| grep ' + exp
child = os.popen(command)
return data

I had to escape the ? and & in the url, or the CGI script at the other
end would refuse to cooperate with "Invalid UserID or UserPW".
--
Leif Biberg Kristensen

Tim Jarman
Guest
Posts: n/a

 02-27-2005
Rigga wrote:

> Pink wrote:
>
>> Rigga wrote:
>>
>>> Hi,
>>>
>>> I am running the line of code below from a shell script and it works
>>> fine, however I am at a total loss on how i can run it from within a
>>> Python script as every option I have tried fails and it appears to be
>>> down to the escaping of certain characters.
>>>
>>> wget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \" | sed -n
>>> 's/.*url="$$[^"]*$$".*/\1/p'

>> If your problem is getting a python string without worrying about how to
>> escape the escape sequences, try:
>>
>> r"""wget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \" | sed -n
>> 's/.*url="$$[^"]*$$".*/\1/p'"""
>>
>> You should be able to pass this directly to a popen() function.

>
> Hi,
>
> Thanks for replying however I have just tried that and it does not seem to
> work, it doesnt return any results (i take it the r was a typo)
>
> Thanks
>
> RiGGa

No, the r was the point - it's there to tell Python not to do any escaping
on the string. Try it again with the r and see what happens.

--
Website: www DOT jarmania FULLSTOP com

Rigga
Guest
Posts: n/a

 02-27-2005
Tim Jarman wrote:

> Rigga wrote:
>
>> Pink wrote:
>>
>>> Rigga wrote:
>>>
>>>> Hi,
>>>>
>>>> I am running the line of code below from a shell script and it works
>>>> fine, however I am at a total loss on how i can run it from within a
>>>> Python script as every option I have tried fails and it appears to be
>>>> down to the escaping of certain characters.
>>>>
>>>> wget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \" | sed -n
>>>> 's/.*url="$$[^"]*$$".*/\1/p'
>>> If your problem is getting a python string without worrying about how to
>>> escape the escape sequences, try:
>>>
>>> r"""wget -q www.anywebpage.com -O - | tr '\r' '\n' | tr \' \" | sed -n
>>> 's/.*url="$$[^"]*$$".*/\1/p'"""
>>>
>>> You should be able to pass this directly to a popen() function.

>>
>> Hi,
>>
>> Thanks for replying however I have just tried that and it does not seem
>> to work, it doesnt return any results (i take it the r was a typo)
>>
>> Thanks
>>
>> RiGGa

>
> No, the r was the point - it's there to tell Python not to do any escaping
> on the string. Try it again with the r and see what happens.
>

Brilliant!!! that works a treat thankyou!!, where on earth did you find out
about the 'r' any pointers to documentation appreciated.

Thanks

RiGGa

Brian van den Broek
Guest
Posts: n/a

 02-27-2005
Rigga said unto the world upon 2005-02-27 15:04:
> Tim Jarman wrote:

<SNIP>

>>No, the r was the point - it's there to tell Python not to do any escaping
>>on the string. Try it again with the r and see what happens.
>>

>
> Brilliant!!! that works a treat thankyou!!, where on earth did you find out
> about the 'r' any pointers to documentation appreciated.
>
> Thanks
>
> RiGGa

<http://www.python.org/doc/current/ref/strings.html>
<http://www.python.org/doc/current/lib/module-re.html>

Best,

Brian vdB

Pink
Guest
Posts: n/a

 02-27-2005
Rigga wrote:
>> No, the r was the point - it's there to tell Python not to do any
>> escaping on the string. Try it again with the r and see what happens.
>>

> Brilliant!!! that works a treat thankyou!!, where on earth did you find
> out
> about the 'r' any pointers to documentation appreciated.

This is a pretty common problem when working with regular expression (which
usually contain many backslashes) - that's where I saw this syntax for the
first time (e.g. http://docs.python.org/lib/match-objects.html).
The official reference for string literals is here:
http://docs.python.org/ref/strings.html

c ya

Rigga
Guest
Posts: n/a

 03-02-2005
Brian van den Broek wrote:

> Rigga said unto the world upon 2005-02-27 15:04:
>> Tim Jarman wrote:

>
> <SNIP>
>
>>>No, the r was the point - it's there to tell Python not to do any
>>>escaping on the string. Try it again with the r and see what happens.
>>>

>>
>> Brilliant!!! that works a treat thankyou!!, where on earth did you find
>> out
>> about the 'r' any pointers to documentation appreciated.
>>
>> Thanks
>>
>> RiGGa

>
> <http://www.python.org/doc/current/ref/strings.html>
> <http://www.python.org/doc/current/lib/module-re.html>
>
> Best,
>
> Brian vdB

Thanks for all your help with this it is appreciated, one further question
though, how do I pass a variable to the external program while using the
r"""

Thanks

RiGGa

Tim Jarman
Guest
Posts: n/a

 03-02-2005
Rigga wrote:

> Brian van den Broek wrote:
>
>> Rigga said unto the world upon 2005-02-27 15:04:

(snip stuff about raw strings)

> Thanks for all your help with this it is appreciated, one further question
> though, how do I pass a variable to the external program while using the
> r"""
>
> Thanks
>
> RiGGa

I'm not sure I understand the question. Say you have:

parameter = r"my \funky \text"

then surely you just pass it to your external program using whichever method
you like, e.g.

import os
os.execl("your_external_prog", parameter) # replaces the current process

or some variant of:

return_code = os.spawnl(os.P_WAIT, "your_external_prog", parameter)

or you can build a command line:

command = "your_external_prog %s" % parameter
return_code = os.system(command)

(see docs on the os module for more variations on this theme than you can
shack a stick at)

It's just a string, after all.

--
Website: www DOT jarmania FULLSTOP com