Best practice prompting for password

2007-09-07 Thread GiBo
Hi,

what's the best practice to securely prompt user for password on console
in Python? IIRC some programs like SSH do a lot to ensure that the input
comes from TTY and is not redirected from somewhere and several other
checks. In the case of OpenSSH even the password prompt is always
displayed regardless if the output is redirected to a file or not.

Any pointers to best practice or code snippet of python password prompt?

Thanks

Gibo
-- 
http://mail.python.org/mailman/listinfo/python-list


How to instantiate a different class in a constructor?

2007-01-23 Thread GiBo
Hi all,

I have a class URI and a bunch of derived sub-classes for example
HttpURI, FtpURI, HttpsURI, etc. (this is an example, I know there is
module urllib & friends, however my actual problem however maps very
well to this example).

Now I want to pass a string to constructor of URI() and get an instance
of one of the subclasses back. For example uri=URI('http://abcd/...')
will make 'uri' an instance of HttpURI class, not instance of URI.

To achieve this I have a list of all subclasses of URI and try to
instantiate one by one in URI.__new__(). In the case I pass e.g. FTP URI
to HttpURI constructor it raises ValueError exception and I want to test
HttpsURI, FtpURI, etc.

For now I have this code:

=
class URI(object):
def __new__(self, arg):
for subclass in subclasses:
try:
instance = object.__new__(subclass, arg)
return instance
except ValueError, e:
print "Ignoring: %s" % e
raise ValueError("URI format not recognized" % arg)

class HttpURI(URI):
def __init__(self, arg):
if not arg.startswith("http://";):
raise ValueError("%s: not a HTTP URI" % arg)
self._uri = arg

class FtpURI(URI):
def __init__(self, arg):
if not arg.startswith("ftp://";):
raise ValueError("%s: not a FTP URI")
self._uri = arg

subclasses = [HttpURI, FtpURI]

if __name__ == "__main__":
print "Testing HTTP URI"
uri = URI("http://server/path";)
print uri

print "Testing FTP URI"
uri = URI("ftp://server/path";)
print uri
=

The problem is that ValueError exception raised in HttpURI.__init__() is
not handled in URI.__new__():

-
~$ ./tst.py
Testing HTTP URI
<__main__.HttpURI object at 0x808572c>  # this is good
Testing FTP URI
Traceback (most recent call last):
  File "./tst.py", line 35, in 
uri = URI("ftp://server/path";)
  File "./tst.py", line 18, in __init__
raise ValueError("%s: not a HTTP URI" % arg)
ValueError: ftp://server/path: not a HTTP URI   # this is bad
-

When I change the __init__ methods of subclasses to __new__ I instead get:

-
./tst.py
Testing HTTP URI
Traceback (most recent call last):
  File "./tst.py", line 29, in 
uri = URI("http://server/path";)
  File "./tst.py", line 7, in __new__
instance = object.__new__(subclass, arg)
TypeError: default __new__ takes no parameters
-

Does anyone have any hints on how to solve this problem? (other than
using urllib or other standard modules - as I said this is just to
demonstrate the nature of my problem).

Thanks!


GiBo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to instantiate a different class in a constructor?

2007-01-23 Thread GiBo
Paul McGuire wrote:
> On Jan 23, 5:09 am, GiBo <[EMAIL PROTECTED]> wrote:
>> Hi all,
>>
>> I have a class URI and a bunch of derived sub-classes for example
>> HttpURI, FtpURI, HttpsURI, etc. (this is an example, I know there is
>> module urllib & friends, however my actual problem however maps very
>> well to this example).
>>
>> Now I want to pass a string to constructor of URI() and get an instance
>> of one of the subclasses back. For example uri=URI('http://abcd/...')
>> will make 'uri' an instance of HttpURI class, not instance of URI.
>>
>> To achieve this I have a list of all subclasses of URI and try to
>> instantiate one by one in URI.__new__(). In the case I pass e.g. FTP URI
>> to HttpURI constructor it raises ValueError exception and I want to test
>> HttpsURI, FtpURI, etc.
>>
>> For now I have this code:
>>
>> =
>> class URI(object):
>> def __new__(self, arg):
>> for subclass in subclasses:
>> try:
>> instance = object.__new__(subclass, arg)
>> return instance
>> except ValueError, e:
>> print "Ignoring: %s" % e
>> raise ValueError("URI format not recognized" % arg)
>>
> 
> 
> 
> Call __new__ and subclass.__init__ explicitly:

Thanks! That's it :-)

BTW When is the subclass.__init__() method invoked if I don't explicitly
call it from __new__()? Apparently not from baseclass.__new__() nor from
object.__new__().

GiBo
-- 
http://mail.python.org/mailman/listinfo/python-list


Checking for EOF in stream

2007-02-19 Thread GiBo
Hi!

Classic situation - I have to process an input stream of unknown length
until a I reach its end (EOF, End Of File). How do I check for EOF? The
input stream can be anything from opened file through sys.stdin to a
network socket. And it's binary and potentially huge (gigabytes), thus
"for line in stream.readlines()" isn't really a way to go.

For now I have roughly:

stream = sys.stdin
while True:
data = stream.read(1024)
process_data(data)
if len(data) < 1024:## (*)
break

I smell a fragile point at (*) because as far as I know e.g. network
sockets streams may return less data than requested even when the socket
is still open.

I'd better like something like:

while not stream.eof():
...

but there is not eof() method :-(

This is probably a trivial problem but I haven't found a decent solution.

Any hints?

Thanks!

GiBo
-- 
http://mail.python.org/mailman/listinfo/python-list


New-style classes (was Re: Checking for EOF in stream)

2007-02-19 Thread GiBo
GiBo wrote:
> Hi!
> 
> Classic situation - I have to process an input stream of unknown length
> until a I reach its end (EOF, End Of File). How do I check for EOF? 
> [...]
> I'd better like something like:
> 
> while not stream.eof():
>   ...

Is there a reason why some classes distributed with Python 2.5 are not
new-style classes? For instance StringIO is apparently "old-style" class
i.e. not inherited from "object". Can I somehow turn an existing
old-style class to a new-style one? I tried for example:
class MyStreamIO(StreamIO, object):
pass
but got an error. Is my only chance taking StringIO.py from python tree,
adding "(object)" to the class declaration and distributing it with my
source?

FWIW I created a wrapper class that does what I need with the EOF
problem in quite elegant way, but due to __getattribute__() stuff it
only works with new-style classes :-(

class MyStreamer(object):
def __init__(self, stream):
self._stream = stream
self.eof = False

def read(self, amount = None):
data = self._stream.read(amount)
if len(data) == 0:
self.eof = True
raise EOFError
return data

def __getattribute__(self, attribute):
try:
return object.__getattribute__(self, attribute)
except AttributeError:
pass
return self._stream.__getattribute__(attribute)


GiBo


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Checking for EOF in stream

2007-02-19 Thread GiBo
Grant Edwards wrote:
> On 2007-02-19, GiBo <[EMAIL PROTECTED]> wrote:
>> Hi!
>>
>> Classic situation - I have to process an input stream of unknown length
>> until a I reach its end (EOF, End Of File). How do I check for EOF? The
>> input stream can be anything from opened file through sys.stdin to a
>> network socket. And it's binary and potentially huge (gigabytes), thus
>> "for line in stream.readlines()" isn't really a way to go.
>>
>> For now I have roughly:
>>
>> stream = sys.stdin
>> while True:
>>  data = stream.read(1024)
> if len(data) == 0:
>  break  #EOF
>>  process_data(data)

Right, not a big difference though. Isn't there a cleaner / more
intuitive way? Like using some wrapper objects around the streams or
something?

GiBo

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: New-style classes (was Re: Checking for EOF in stream)

2007-02-19 Thread GiBo
Gabriel Genellina wrote:
> En Mon, 19 Feb 2007 22:30:59 -0300, GiBo <[EMAIL PROTECTED]> escribió:
> 
>> Is there a reason why some classes distributed with Python 2.5 are not
>> new-style classes? For instance StringIO is apparently "old-style" class
>> i.e. not inherited from "object". Can I somehow turn an existing
>> old-style class to a new-style one? I tried for example:
>>  class MyStreamIO(StreamIO, object):
>>  pass
>> but got an error.
> 
> Try again (and look carefully the error text)

Ahhh, thanks! ;-)

Just for the record:

import StringIO
class MyStringIO(StringIO, object):
pass

must of course be:

class MyStringIO(StringIO.StringIO, object):
pass

One more question - is it likely that StringIO will be turned into
new-style class in the future? The reason I ask is whether I should try
to deal with detection of new-/old-style classes or take the
old-styleness for granted and set in stone instead.

GiBo
-- 
http://mail.python.org/mailman/listinfo/python-list


Which Crypto Library?

2007-02-21 Thread GiBo
Hi

I need some encryption done in my Python 2.5 application. I wonder
what's the most recommended library? I've found M2crypto and some
OpenSSL wrappers and Python Cryptography Toolkit and some others. No
surprise I'm confused :-)

What's the most often used library for crypto?

For now I need a simple AES, no asymmetric crypto or GPG involved.

Thanks!

GiBo



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: guess file type in python

2007-02-21 Thread GiBo
mark wrote:
> Is there any way to guess the file type using python?
> thanks
> mark
> 
> For example in unix:
> 
> file code.py
> code.py: a python script text executable
> 
> file Eccentric.gif
> Eccentric.gif: GIF image data, version 89a, 237 x 277

"file" tool uses libmagic.so library which in turn uses /etc/magic file
with file format descriptions. To get some reliable results you'll have
to call libmagic. The "file-4.xx.tar.gz" source tarball has python
bindings included and there's a python-magic package (at least in
Debian) that provides libmagic bindings for python.

HTH,

GiBo



-- 
http://mail.python.org/mailman/listinfo/python-list