Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Need help running external program

Reply
Thread Tools

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




 
Reply With Quote
 
 
 
 
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.

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
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)
data = child.read()
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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

 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about running external program Zhao Yi Ruby 4 01-07-2009 01:03 PM
Python 2.5 fails where 2.4 works when running external program Heikki Toivonen Python 2 03-30-2007 07:36 AM
Capture file descriptors while running an external program jfigueiras@sapo.pt Python 1 11-25-2006 08:15 PM
Capture file descriptors while running an external program jfigueiras@sapo.pt Python 0 11-22-2006 12:40 PM
running a external program on server side wakun@wakun.com ASP .Net 2 03-13-2006 02:50 PM



Advertisments