Python-list Digest, Vol 37, Issue 23
Mark Devine
mfmdevine at gmail.com
Mon Oct 2 11:14:21 EDT 2006
Hi
Sorry about that. Here is the full question:
I wonder if you can help me. I am using pexpect with python to access remote
machines and run commands on them. I pass commands into code like so:
def cmd(self, cmd):
pp=[ "|", "]", "[", "(", ")", "$", "?", "*", ".", ":"]
expcmd=cmd
for element in pp:
expcmd=expcmd.replace(element, "\\%s" % element)
self.spawn.setecho(False)
self.spawn.sendline(cmd)
self.spawn.expect(expcmd)
self.spawn.expect(self.prompt)
return self.spawn.before
The above code is supposed to match the command, then match the prompt and
then return what is in between. Since pexpect uses regular expressions to
match what it sees, the command needs to have certain characters
backslashed. The problem is that on some remote shells hidden characters are
introduced that causes the expect statement (self.spawn.expect(expcmd)) to
timeout without matching. In perl there is a way to translate any character
with an ascii value of less than 32 to "" so that all hidden characters are
removed. Can this be done in python? Can this be applied to the regular
expression used by pexpect?
Bewildered
On 02/10/06, python-list-request at python.org <python-list-request at python.org>
wrote:
>
> Send Python-list mailing list submissions to
> python-list at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://mail.python.org/mailman/listinfo/python-list
> or, via email, send a message with subject or body 'help' to
> python-list-request at python.org
>
> You can reach the person managing the list at
> python-list-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Python-list digest..."
>
>
> Today's Topics:
>
> 1. Re: Problems wth os.stat().st_mtime on Mac (Michael Glassford)
> 2. Re: Help with ConfigParser (TonyHa)
> 3. regular expressions in the pexpect module for python (Mark Devine)
> 4. commands.getstatusoutput result is not command line exit
> value!!! (Hari Sekhon)
> 5. PyCon proposals (was Re: PATCH: Speed up direct string
> concatenation by 20+%!) (Aahz)
> 6. Re: Python/UNO/OpenOffice? (John Machin)
> 7. Re: Help with ConfigParser (Enrico)
> 8. How to coerce a list of vars into a new type? (Matthew Wilson)
> 9. Making posts to an ASP.NET webform. (Bernard)
> 10. How can I make a class that can be converted into an int?
> (Matthew Wilson)
> 11. Sort by domain name? (js )
>
>
>
> ---------- Forwarded message ----------
> From: Michael Glassford <glassfordm at hotmail.com>
> To: python-list at python.org
> Date: Mon, 02 Oct 2006 10:15:26 -0400
> Subject: Re: Problems wth os.stat().st_mtime on Mac
> Martin v. Löwis wrote:
> > Michael Glassford schrieb:
> >> Although not mentioned in the Python 2.5 News, apparently there was a
> >> similar change on Mac that I'm having some problems with. On the Mac,
> >> just as on Windows, os.stat().st_mtime now returns a float instead of
> an
> >> integer.
> >
> > It's isn't really new; os.stat_float_times was introduced in Python 2.3.
> > What changed in 2.5 is that it is now true. See
> >
> > http://docs.python.org/whatsnew/modules.html
>
> Thanks, I wasn't aware of os.stat_float_times. This helps me a lot,
> since I can turn off the floating point times in places until
> incompatible code can be fixed.
>
> >
> >> a) Why the difference between machines?
> >
> > You really have to delve into OSX to answer this question. Several
> > reasons are possible:
> > - there is a change in the operating system implementations
>
> Possible, I guess, although I don't know how to find out and there's
> likely nothing I could do about it even if I did.
>
> > - you are using different Python versions
>
> Python 2.5 on both.
>
> > - you are using different file systems
>
> This is the first thing I thought of, but all of the drives are
> formatted using "Mac OS Extended (Journalled)", which I assumed meant
> they are using the same file system.
>
> >
> >> b) Why do most files on this machine have ".0", while some (generally
> >> those I created myself using Python 2.5, I think) don't?
> >
> > Hard to tell. Maybe the software that created those files explicitly
> > set a time stamp on them, and failed to use the API that supports
> > subsecond resolution in setting those time stamps.
> >
> >> I understand how the results can be different: the os.stat() function
> >> returns a posix.stat_result object, which gives back an integer value
> >> for the mtime if you call __str__ or __repr__, or if you iterate on it;
> >> and a float if you get the st_mtime attribute. But I would consider it
> a
> >> bug that the results are different: a float should be returned in
> either
> >> case.
> >
> > That's for backwards compatibility: You shouldn't use the tuple
> > interface anymore, but use st_mtime for new code. This will always
> > be a float. OTOH, the tuple interface will continue to return
> > integers forever
>
> <snip>
>
> OK, thanks for the explanation.
>
> Mike
>
>
>
>
>
> ---------- Forwarded message ----------
> From: "TonyHa" <tony_ha2002 at yahoo.co.uk>
> To: python-list at python.org
> Date: 2 Oct 2006 07:29:49 -0700
> Subject: Re: Help with ConfigParser
> Hello Peter,
>
> Thanks for your help, and it works now!
>
> Tony.
>
> Peter Otten wrote:
> > tony.ha at philips.com wrote:
> >
> > > Question: How can I pervent ConfigParse to convert Upper case yo lower
> > > case??, thanks.
> >
> > http://docs.python.org/dev/lib/RawConfigParser-objects.html
> >
> > """
> > optionxform(option)
> >
> > Transforms the option name option as found in an input file or as passed
> in
> > by client code to the form that should be used in the internal
> structures.
> > The default implementation returns a lower-case version of option;
> > subclasses may override this or client code can set an attribute of this
> > name on instances to affect this behavior. Setting this to str(), for
> > example, would make option names case sensitive.
> > """"
> >
> > If you don't pass defaults:
> >
> > config = ConfigParser()
> > config.optionxform = str
> > # ...
> >
> > Or, to be on the safe side:
> >
> > class MyCasePreservingConfigParser(ConfigParser):
> > optionxform = str
> >
> > config = MyCasePreservingConfigParser()
> > # ...
> >
> > Peter
>
>
>
>
>
> ---------- Forwarded message ----------
> From: "Mark Devine" <mfmdevine at gmail.com>
> To: python-list at python.org
> Date: Mon, 2 Oct 2006 15:43:35 +0100
> Subject: regular expressions in the pexpect module for python
> Hi
> I wonder if you can help me. I am using pexpect with python to access
> remote machines and run commands on them. I pass commands into code like so:
>
> def cmd(self, cmd):
> pp=[ "|", "]", "[", "(", ")", "$", "?", "*", ".", ":"]
> expcmd=cmd
> for element in pp:
> expcmd=expcmd.replace(element, "\\%s" % element)
> self.spawn.setecho(False)
> self.spawn.sendline(cmd)
> self.spawn.expect (expcmd)
> self.spawn.expect(self.prompt)
> return self.spawn.before
>
> The above code is supposed to match the command, then match the prompt and
> then return what is in between. Since pexpect uses regular expressions to
> match what it sees
>
>
> ---------- Forwarded message ----------
> From: Hari Sekhon <hpsekhon at googlemail.com>
> To: python-list at python.org
> Date: Mon, 02 Oct 2006 15:45:12 +0100
> Subject: commands.getstatusoutput result is not command line exit value!!!
> I'm running a command like
>
> import commands
> result = commands.getstatusoutput('somecommand')
> print result[0]
> 3072
>
>
> However, this exit code made no sense so I ran it manually from the
> command line in bash on my linux server and it gives the exit code as
> 12, not this weird 3072 number.
>
> So I tried os.system('somecommand') in the interactive python shell and
> it too returned the same result for the exit code as the unix shell, 12,
> but re-running the commands.getstatusoutput() with the exact same
> command still gave 3072.
>
>
> Is commands.getstatusoutput() broken or something?
>
>
> -h
>
> --
> Hari Sekhon
>
>
>
>
>
> ---------- Forwarded message ----------
> From: aahz at pythoncraft.com (Aahz)
> To: python-list at python.org
> Date: 2 Oct 2006 07:43:09 -0700
> Subject: PyCon proposals (was Re: PATCH: Speed up direct string
> concatenation by 20+%!)
> In article <1159762765.943691.139690 at b28g2000cwb.googlegroups.com>,
> Larry Hastings <larry at hastings.org> wrote:
> >Steve Holden wrote:
> >>
> >> I think your project might make a very
> >> interesting PyCon paper for people who were thinking about joining the
> >> development effort but hadn't yet started.
> >
> >Perhaps; I've never been to PyCon, but it might be fun to give a
> >presentation there. That said, it would be way more relevant if the
> >patch got accepted, don'tcha think?
>
> Not really. The principles involved are timeless, and I guarantee you a
> large audience regardless of whether the patch gets accepted (provided
> you specify an appropriate presentation title and summary).
> --
> Aahz (aahz at pythoncraft.com) <*>
> http://www.pythoncraft.com/
>
> "LL YR VWL R BLNG T S" -- www.nancybuttons.com
>
>
>
>
> ---------- Forwarded message ----------
> From: "John Machin" <sjmachin at lexicon.net>
> To: python-list at python.org
> Date: 2 Oct 2006 07:46:53 -0700
> Subject: Re: Python/UNO/OpenOffice?
>
> olive wrote:
> > ... and you have to start your py file with:
> >
> > import uno, sys, socket
> > from com.sun.star.beans import PropertyValue
> >
> > ... and your start_oo_server.bat file with:
> >
> > @SET PYTHONPATH=C:\Program Files\OpenOffice.org 2.0\program;C:\Program
> > Files\OpenOffice.org 2.0\program\python-core-2.3.4\lib
> > @SET PATH=C:\Program Files\OpenOffice.org 2.0\program;C:\Program
> > Files\OpenOffice.org 2.0\program\python-core-2.3.4\bin
>
> Many thanks for all that, olive; I made the minimal hacks to make it
> open an XLS ffile, and it worked!
> I'll try to see why that worked and my previous experiment crashed
> inside a DLL.
> Cheers,
> John
>
>
>
>
>
> ---------- Forwarded message ----------
> From: "Enrico" <lux at perpetua.org>
> To: python-list at python.org
> Date: Mon, 2 Oct 2006 16:53:04 +0200
> Subject: Re: Help with ConfigParser
> Hi,
> from the documentation:
>
> optionxform(option)
>
> Transforms the option name option as found in an input file or as passed
> in
> by client code to the form that should be used in the internal structures.
> The default implementation returns a lower-case version of option;
> subclasses may override this or client code can set an attribute of this
> name on instances to affect this behavior. Setting this to str(), for
> example, would make option names case sensitive.
>
> Bye,
> Enrico
>
>
>
>
>
>
> ---------- Forwarded message ----------
> From: Matthew Wilson <matt at tplus1.com>
> To: python-list at python.org
> Date: Mon, 02 Oct 2006 14:53:10 GMT
> Subject: How to coerce a list of vars into a new type?
>
> I want to verify that three parameters can all be converted into
> integers, but I don't want to modify the parameters themselves.
>
> This seems to work:
>
> def f(a, b, c):
>
> a, b, c = [int(x) for x in (a, b, c)]
>
> Originally, I had a bunch of assert isinstance(a, int) statements at the
> top of my code, but I decided that I would rather just check if the
> parameters can be converted into integers.
>
> The new a, b, and c are all different objects, with different id values.
> Anyhow, this all seems like black magic to me. Can anyone explain what
> is going on?
>
> Is it as simple as call-by-value?
>
>
>
>
> --
> A better way of running series of SAS programs:
> http://overlook.homelinux.net/wilsonwiki/SasAndMakefiles
>
>
>
>
> ---------- Forwarded message ----------
> From: "Bernard" <bernard.chhun at gmail.com>
> To: python-list at python.org
> Date: 2 Oct 2006 07:55:23 -0700
> Subject: Making posts to an ASP.NET webform.
> hiya everyone,
>
> I've made this little webcrawler using BeautifulSoup, urllib and
> urllib2. I've been encountering ASP.NET Forms recently and I can't seem
> to make a proper post to some of them. My post function has been doing
> great until this particular website.
> Here's some explanations first so that you guys understands my
> problem...
>
> Usually there are 8 hidden inputs spread all over the web page:
> __EVENTTARGET --> that is the id of the control that is assigned by the
> ASP.NET engine. It is usually in this particular form
> "dg_Result$_ctl2$linkbtn1" The javascript function DoPostback usually
> replaces the '$' for a ':' before doing the real post. Some other
> ASP.NET page doesn't.
> __EVENTARGUMENT --> nothing's here usually.
> __ScrollTop --> its value is often 0
> __ScrollLeft --> same as __ScrollTop
> __ValidationSummary --> usually empty
> __VIEWSTATEENCRYPTED --> an encrypted string. I don't know what it
> means nor what it does.
> __EVENTVALIDATION --> an encrypted string. I don't know what it means
> nor what it does.
> __VIEWSTATE --> the encrypted state of the current control. the state
> of a datagrid for example.
>
> I extract all those values using regexes on the web page, build a
> dictionnary out of all those values and encrypt the whole thing using
> urllib.urlencode(). Afterwards I extract the form action and post the
> whole thing with cookies enabled. This procedure works like a charm.
>
> The website I've just encounted has only 3 of the 8 hidden inputs.
> (EVENTTARGET, EVENTARGUMENT & VIEWSTATE ).
> I tried posting the whole thing using only these 3 values & nada.
> I then tried posting the whole thing using all empty values for the 5
> remaining values...still nothing.
>
> Has anyone tried what I'm doing? and if you tried how have you
> succeeded getting the data back after the post action?
>
> thanks for any tips!
>
> cP
>
>
>
>
>
> ---------- Forwarded message ----------
> From: Matthew Wilson <matt at tplus1.com>
> To: python-list at python.org
> Date: Mon, 02 Oct 2006 14:59:05 GMT
> Subject: How can I make a class that can be converted into an int?
> What are the internal methods that I need to define on any class so that
> this code can work?
>
> c = C("three")
>
> i = int(c) # i is 3
>
> I can handle the part of mapping "three" to 3, but I don't know what
> internal method is called when int(c) happens.
>
> For string conversion, I just define the __str__ method. What's the
> equivalent for int? For float, too, while I'm at it?
>
> TIA
>
> Matt
>
> --
> A better way of running series of SAS programs:
> http://overlook.homelinux.net/wilsonwiki/SasAndMakefiles
>
>
>
>
> ---------- Forwarded message ----------
> From: "js " <ebgssth at gmail.com>
> To: python-list at python.org
> Date: Tue, 3 Oct 2006 00:08:06 +0900
> Subject: Sort by domain name?
> Hi list,
>
> I have a list of URL and I want to sort that list by the domain name.
>
> Here, domain name doesn't contain subdomain,
> or should I say, domain's part of 'www', mail, news and en should be
> excluded.
>
> For example, if the list was the following
> ------------------------------------------------------------
> http://mail.google.com
> http://reader.google.com
> http://mail.yahoo.co.uk
> http://google.com
> http://mail.yahoo.com
> ------------------------------------------------------------
>
> the sort's output would be
> ------------------------------------------------------------
> http://google.com
> http://mail.google.com
> http://reader.google.com
> http://mail.yahoo.co.uk
> http://mail.yahoo.com
> ------------------------------------------------------------
>
> As you can see above, I don't want to
>
>
> Thanks in advance.
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20061002/2bdf730a/attachment.html>
More information about the Python-list
mailing list