EOL for sys.stdin.readline() and raw_input()

2009-02-26 Thread jkv

Hi,

Are there any way to change the EOL character for sys.stdin.readline() 
and raw_input()?
My problem is that i cannot rely on that the clients connection to my 
application will end all lines with \n or \r\n. Sometimes they will use 
\r\000 as EOL.


Example from my code:

   sys.stdout.write('Password: ');
   #IAC DO ECHO  
   sys.stdout.write('\xFF\xFB\x01')

   password = raw_input()

If the client sends 'qwerty\r\n' everything is fine and the password get 
stored in the variable. But sometimes, depending on how broken the 
telnet client are, the client will send 'qwerty\r\000' instead and then 
my application will hang at 'password = raw_input()' forever.


Any suggestions?

Regards,
jkv


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


Re: EOL for sys.stdin.readline() and raw_input()

2009-02-26 Thread jkv

Terry Reedy wrote:

jkv wrote:

Hi,

Are there any way to change the EOL character for 
sys.stdin.readline() and raw_input()?
My problem is that i cannot rely on that the clients connection to my 
application will end all lines with \n or \r\n. Sometimes they will 
use \r\000 as EOL.


Example from my code:

   sys.stdout.write('Password: ');
   #IAC DO ECHO sys.stdout.write('\xFF\xFB\x01')
   password = raw_input()

If the client sends 'qwerty\r\n' everything is fine and the password 
get stored in the variable. But sometimes, depending on how broken 
the telnet client are, the client will send 'qwerty\r\000' instead 
and then my application will hang at 'password = raw_input()' forever.


Can you put that in a separate thread with timeout?

I guess i could - wouldn't know how thou, just getting started in python.

Is there any way i can read stdin character by character? In C i would 
loop over 'ch = getch();' and in each iteration push the character to 
the password variable unless  ch equals to \r.

Anyway to do something like this in python?

Regards,
jkv

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


Re: EOL for sys.stdin.readline() and raw_input()

2009-02-26 Thread jkv

Jean-Paul Calderone wrote:

   sys.stdout.write('Password: ');

   #IAC DO ECHO sys.stdout.write('\xFF\xFB\x01')
   password = raw_input()

If the client sends 'qwerty\r\n' everything is fine and the password 
get stored in the variable. But sometimes, depending on how broken 
the telnet client are, the client will send 'qwerty\r\000' instead 
and then my application will hang at 'password = raw_input()' forever.


Any suggestions?


It sounds like you might want to use a real implementation of the telnet
protocol in your application.  Such an implementation would deal with the
fact that telnet encodes \r as \r\0 for you.

Twisted includes a telnet implementation, as well as facilities for 
reading

stdin asynchronously (which will let you avoid indefinite hangs).
I am running this script inside a wrapper (honeyd), so i cant really use 
twisted since i don't have raw network access - i only got stdout, stdin 
and stderr to play with.


But i found a solution, posting it here for the archives:

password = ""
while 1:
  c = sys.stdin.read(1)
  if c == '\r' or c == '\n':
  break
  password = password + c



Regards,
jkv
--
http://mail.python.org/mailman/listinfo/python-list


Re: Q: finding distance between 2 time's

2009-05-30 Thread jkv
Hi Martin,

What i usally do is to convert the two times to seconds since epoch and
compare the two values in seconds. You can use time.mktime to convert
localtime to seconds since epoch.

I added a few lines to your script, and now it ought to only print files
newer than 3601 seconds (3600 seconds is one hour).
The modification to you script is just some quick code, im pretty sure
on can compress it a bit.

Regards,
Johnny

script:

import os, time

def buildList(directory):
listing = os.listdir(directory)
for x in listing:
x = os.path.join(directory, x)
if os.path.isdir(x):
print ('dir -> %s') % x
if os.path.isfile(x):
current_time = time.localtime()
tstF = time.localtime(os.path.getmtime(x))
#convert the current time to seconds since epoch
current_epoch=time.mktime(current_time)
#convert the timestamp on the file to seconds since epoch
file_epoch = time.mktime(tstF)
nSize = os.path.getsize(x)
time_difference = current_epoch - file_epoch
#if file newer than one hour print a line
if time_difference < 3601:
  print ('NEW FILE -> %s %d @ %s') % (x, nSize, time.asctime
(tstF))
   return 0

tstN = time.localtime()
print tstN
print "Time now: %s" % time.asctime(tstN)
buildList('/')


mar...@hvidberg.net wrote:
> I made this little script (below) to look througt a dir to see if
> there are any files newer than .e.g. 1 hour.
> I have the loop through the dir working and can retreive file time as
> well as present time.
> both time variables are in the format returned by time.localtime()
>
> My question:
> How do I find the difference between such two time variables, to
> calculate the 'age' of the file?
>
> :-) Martin
>
> --8<-- Code begin -
>
> import os, time
>
> def buildList(directory):
> listing = os.listdir(directory)
> for x in listing:
> x = os.path.join(directory, x)
> if os.path.isdir(x):
> print ('dir -> %s') % x
> if os.path.isfile(x):
> tstF = time.localtime(os.path.getmtime(x))
> nSize = os.path.getsize(x)
> print ('fil -> %s %d @ %s') % (x, nSize, time.asctime
> (tstF))
> return 0
>
> tstN = time.localtime()
> print tstN
> print "Time now: %s" % time.asctime(tstN)
> buildList('C:\Martin\Work\Work_Eclipse\.metadata')
>
>
>   


-- 
Regards,
jkv
http://unixcluster.dk/public.key

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


Re: Q: finding distance between 2 time's

2009-05-30 Thread jkv
mar...@hvidberg.net wrote:
> Thanks both
>
> The first answer is quite instuctive, the other one might be the one
> I'll use in t
I didn't receive the other answer, could you please forward it to me?
> So 2x thanks.
You are welcome.

I took another look at your code, and you can compress it all to a if
"oneliner":
(and thanks to Steven for the <= reminder)

if os.path.isfile(x):
nSize = os.path.getsize(x)
#if oneliner
if time.mktime(time.localtime()) -
time.mktime(time.localtime(os.path.getmtime(x))) <= 3600:
  print ('HT fil -> %s %d @ %s') % (x, nSize, time.asctime)

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


Re: csv blank fields

2009-06-27 Thread jkv
Mag Gam wrote:

> > well, I am actually loading the row into a fixed width array
> >
> > reader=csv.reader(fs)
> > for s,row in enumerate(reader):
> >   
> > t=np.array([(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10])],dtype=mtype)
> >   d[s]=t
> >
> >
> > If there is a missing field, I get a problem in one of my rows
> >   
>   

I had a similar problem, my problem was that in my source data (csv
file) sometimes the last cell was left out, so i ended up doing a try to
check if the row existed, if not i appended to the row.

Something like:

try:
row[11]
except IndexError:
row.append("")

If you want to insert a value into empty cells you can do something like
this:

if row[10] == ''
row[10] = 'NULL'



-- 
Regards,
jkv
http://unixcluster.dk/public.key

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