newbie question About O'Reilly "Python for Unix and Linux System Administration" ftp Mirror question

2012-09-16 Thread moonhkt
Hi All


O'Reilly Book ISBN 978-986-6840-36-4.

python --version
Python 2.6.2 on AIX 5.3

Using this python to get files in ftp server.

I got below error. What is the error meaning and how to fix ?

ftp_mirror.py
Traceback (most recent call last):
  File "/xx../shell/ftpmirror.py", line 80, in 
f = FTPSync(options.host, options.username, options.password,
options.remote_dir, options.local_dir, opti
ons.delete)
  File "xx../shell/ftpmirror.py", line 17, in __init__
self.conn.cwd(ftp_base_dir)
  File "/opt/freeware/lib/python2.6/ftplib.py", line 536, in cwd
cmd = 'CWD ' + dirname
TypeError: cannot concatenate 'str' and 'NoneType' objects

Source :

#!/usr/bin/env python

import ftplib
import os

class FTPSync(object):
def __init__(self, host, username, password, ftp_base_dir,
local_base_dir, delete=False):
self.host = host
self.username = username
self.password = password
self.ftp_base_dir = ftp_base_dir
self.local_base_dir = local_base_dir
self.delete = delete

self.conn = ftplib.FTP(host, username, password)
self.conn.cwd(ftp_base_dir)
try:
os.makedirs(local_base_dir)
except OSError:
pass
os.chdir(local_base_dir)
def get_dirs_files(self):
dir_res = []
self.conn.dir('.', dir_res.append)
files = [f.split(None, 8)[-1] for f in dir_res if
f.startswith('-')]
dirs = [f.split(None, 8)[-1] for f in dir_res if
f.startswith('d')]
return (files, dirs)
def walk(self, next_dir):
print "Walking to", next_dir
self.conn.cwd(next_dir)
try:
os.mkdir(next_dir)
except OSError:
pass
os.chdir(next_dir)

ftp_curr_dir = self.conn.pwd()
local_curr_dir = os.getcwd()

files, dirs = self.get_dirs_files()
print "FILES:", files
print "DIRS:", dirs
for f in files:
print next_dir, ':', f
outf = open(f, 'wb')
try:
self.conn.retrbinary('RETR %s' % f, outf.write)
finally:
outf.close()
if self.delete:
print "Deleting", f
self.conn.delete(f)
for d in dirs:
os.chdir(local_curr_dir)
self.conn.cwd(ftp_curr_dir)
self.walk(d)

def run(self):
self.walk('.')


if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-o", "--host", dest="host",
 action='store', help="FTP host")
parser.add_option("-u", "--username", dest="username",
 action='store', help="FTP username")
parser.add_option("-p", "--password", dest="password",
 action='store', help="FTP password")
parser.add_option("-r", "--remote_dir", dest="remote_dir",
 action='store', help="FTP remote starting directory")
parser.add_option("-l", "--local_dir", dest="local_dir",
 action='store', help="Local starting directory")
parser.add_option("-d", "--delete", dest="delete", default=False,
 action='store_true', help="use regex parser")

(options, args) = parser.parse_args()
f = FTPSync(options.host, options.username, options.password,
options.remote_dir, options.local_dir, options.delete)
f.run()

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


how to build Object for List data

2012-10-08 Thread moonhkt
Hi All

I have Data call FM01. Each format have F1.. F50 Fields. And Global
Program G1..Gn.

The format like below as text file

FM01

Fld #FieldValidation
1F1  N/A
2F2  N/A
3F3  Program1,1,2,3 # Add F1 and F2 value to F3
4F4  Program2,1,3,4 # Add F1 and F3 value to F4
...
50


Seq  validation
1   Program3,1,3,4  # max(F1,F3) to F4
..
n
How to using python to Read the text file, Build the data as object
class ?

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


skip Trackback error for ftp checking

2012-11-09 Thread moonhkt
HI All

How to skip Trackback warning/error when input ftp address is not
correct or reject ?

AIX 5.3

from ftplib import FTP
import ftplib
import sys
from optparse import OptionParser

parser = OptionParser()

parser.add_option("-a","--remote_host_address",
dest="remote_host_address",
help="REMOTE FTP HOST.",metavar="REMOTE FTP HOST")

parser.add_option("-u","--username", dest="username",
help="USERNAME for ftp sever.",metavar="USERNAME")

parser.add_option("-p","--password", dest="password",
help="PASSWORD for ftp server.",metavar="PASSWORD")

(options, args ) = parser.parse_args ()

if not (options.remote_host_address):
parser.error("REMOTE HOST are mandatory")

if options.username and not options.password:
parser.error("PASSWORD is mandatory if USERNAME is present")

try:
   ftp = FTP(options.remote_host_address)
except ftplib.error_perm,e:
   sys.exit(2)

if options.username:
   try:
  ftp.login(options.username,options.password)
   except ftplib.error_perm,e:
  print "Login failed: %s" % e
  sys.exit(1)
else:
   try:
  ftp.login()
   except ftplib.error_perm,e:
  print "Anonymous login failed: %s" % e
  sys.exit(1)
try:
print "LOGIN OK"
finally:
ftp.close()

Command line
---
chkftp.py -a teseting

Output as below

Traceback (most recent call last):
  File "...chkftp.py", line 33, in 
ftp = FTP(options.remote_host_address)
  File "/opt/freeware/lib/python2.6/ftplib.py", line 116, in __init__
self.connect(host)
  File "/opt/freeware/lib/python2.6/ftplib.py", line 131, in connect
self.sock = socket.create_connection((self.host, self.port),
self.timeout)
  File "/opt/freeware/lib/python2.6/socket.py", line 498, in
create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 2] temporary failure in name resolution.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: skip Trackback error for ftp checking

2012-11-12 Thread moonhkt
On Nov 10, 2:50 pm, Steven D'Aprano  wrote:
> On Fri, 09 Nov 2012 20:51:47 -0800, moonhkt wrote:
> > HI All
>
> > How to skip Trackback warning/error when input ftp address is not
> > correct or reject ?
>
> The same way you would skip any other error when you do something wrong:
> catch the exception.
>
> --
> Steven

Thank. Added below.
try:
   ftp = FTP(options.remote_host_address)
except :
  print "Host address not found."
  sys.exit(1)
-- 
http://mail.python.org/mailman/listinfo/python-list


using smtp sent large file upto 60MB

2012-12-04 Thread moonhkt
Hi All

How to using python send file uptp 60MB ?


s = smtplib.SMTP("localhost")
#~~ s.set_debuglevel(1)
s.sendmail(from_addr, to_addr,m.as_string())
s.quit


For 13MB file have below error
s.sendmail(from_addr, to_addr,m.as_string())
  File "/opt/freeware/lib/python2.6/email/message.py", line 135, in
as_string
g.flatten(self, unixfrom=unixfrom)
  File "/opt/freeware/lib/python2.6/email/generator.py", line 84, in
flatten
self._write(msg)
  File "/opt/freeware/lib/python2.6/email/generator.py", line 119, in
_write
self._fp.write(sfp.getvalue())
MemoryError: out of memory
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using smtp sent large file upto 60MB

2012-12-04 Thread moonhkt
On Dec 4, 6:07 pm, Chris Angelico  wrote:
> On Tue, Dec 4, 2012 at 7:15 PM, moonhkt  wrote:
> > How to using python send file uptp 60MB ?
>
> Step one: Don't. SMTP is almost never the best choice for sending huge
> files around.
>
> There are plenty of other ways to share files; send an email with
> instructions on how to access the file, rather than attaching the
> file. For general consumption, the easiest way is usually to include a
> URL for HTTP download. If it's something internal, you might want to
> put the file on a shared-access FTP server or file share.
>
> ChrisA

Thank for suggestion. The next task will be ftp to user folder. But
first tasks is how to using python send huge files.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using smtp sent large file upto 60MB

2012-12-04 Thread moonhkt
On 12月5日, 上午4時54分, Chris Angelico  wrote:
> On Wed, Dec 5, 2012 at 2:41 AM, Laszlo Nagy  wrote:
> > If you still don't want to accept this suggestion, then go ahead! Write a
> > program, send out 100MB emails, and you will see for yourself that it just
> > doesn't work.
>
> But be aware of a few things.
>
> 1) Converting 1MB of binary data into a MIME-packaged email is going
> to result in about 2MB of text. (It's about 1.5MB for base 64
> encoding, which is one of the most common used, plus a bit more for
> structure around it, and rounding up, call it two meg.)
>
> 2) If that 2MB of text is stored as a Python text string, it could
> potentially consume 4MB or 8MB of memory, unless you're on Python 3.3,
> in which case it will be only 2MB..
>
> 3) That 2-8MB has to be contiguous.
>
> 4) Any manipulation of the resulting string - which will quite
> probably happen as it's built, as it gets connected to the email, etc,
> etc, etc - will require even more copies of the string.
>
> So all in all, you need a LOT of memory to do your encoding. That's
> why you're seeing MemoryError - it is simply impossible to attach a
> huge file to an email without using a fair amount of memory. (It's
> possible to use that memory a bit at a time, but since emails are
> generally small, most encoding libraries won't be written to do that.
> This isn't like movie editing, where it's common to work with files
> larger than your RAM.)
>
> ChrisA

Thank for your suggestion.

Machine : AIX
Python version : 2.6.2

I am prepare change UNIX script to Python. smtp and ftp are my first
tasks.

But, when using standard unix command mail and uuencode without this
issue.

Our SMTP can send file more than 60MB. But our notes server can
configured 100MB,30MB or 10MB. My notes Mail box can receive 100MB.

In UNIX, by below command send  smtp mail.
uuencode $xfn $xfn | mail -s "$SUBJECT" $NAME

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


Re: using smtp sent large file upto 60MB

2012-12-04 Thread moonhkt
On 12月5日, 下午1時34分, Chris Angelico  wrote:
> On Wed, Dec 5, 2012 at 11:54 AM, moonhkt  wrote:
> > I am prepare change UNIX script to Python. smtp and ftp are my first
> > tasks.
>
> > But, when using standard unix command mail and uuencode without this
> > issue.
>
> > Our SMTP can send file more than 60MB. But our notes server can
> > configured 100MB,30MB or 10MB. My notes Mail box can receive 100MB.
>
> > In UNIX, by below command send  smtp mail.
> > uuencode $xfn $xfn | mail -s "$SUBJECT" $NAME
>
> Yes, and it is possible to send that much content via SMTP. It just
> isn't something that library authors are going to be overly concerned
> about. You may need to jump through a few extra hoops, or maybe just
> throw more RAM at the computer (possibly switching to a 64-bit build
> of Python if you aren't already using one). However, I would *still*
> recommend using a different transport for such large files.
>
> ChrisA

Thank a lot. We still using Python version : 2.6.2  on AIX 5.3

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


How to list a file which already created a 2 mins ago

2012-12-06 Thread moonhkt
Hi All

AIX.5.3
Python 2.6.2

File ftp to Machine A, need to rename then send to Machine B.

How to list a file which already created a 2 mins ago ?  If file aging
more than 2 mins. I want to rename file to other file name.

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


Re: using smtp sent large file upto 60MB

2012-12-10 Thread moonhkt
On 12月5日, 下午11時01分, Michael Torrie  wrote:
> On 12/04/2012 05:54 PM, moonhkt wrote:
>
> > Our SMTP can send file more than 60MB. But our notes server can
> > configured 100MB,30MB or 10MB. My notes Mail box can receive 100MB.
>
> > In UNIX, by below command send  smtp mail.
> > uuencode $xfn $xfn | mail -s "$SUBJECT" $NAME
>
> Just continue to use this set of commands.  You can use the subprocess
> module to interact with these programs.

OK. Will try using subprocess.
-- 
http://mail.python.org/mailman/listinfo/python-list


About open file for Read

2012-12-10 Thread moonhkt
Hi All

I am new in Python. When using open and then for line in f .

Does it read all the data into f object ? or read line by line ?


  f=open(file, 'r')
   for line in f:
  if userstring in line:
 print "file: " + os.path.join(root,file)
 break
   f.close()


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


About UNIX shell trap, any relative function in Python ?

2012-12-15 Thread moonhkt

Hi All

Machine : AIX 5.3
Python : 2.6.2

In UNIX have, trap to run defined CLEAN_UP function. When  HUP INT
KILL STOP TERM will run CLEAN_UP function.

trap 'echo "\n\nProcessing Clean up"; CLEAN_UP; exit' HUP INT KILL
STOP TERM

Any relative function in Python ?

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


Re: About UNIX shell trap, any relative function in Python ?

2012-12-15 Thread moonhkt
On 12月16日, 上午12時05分, Chris Angelico  wrote:
> On Sun, Dec 16, 2012 at 3:04 AM, Chris Angelico  wrote:
> > On Sun, Dec 16, 2012 at 2:34 AM, moonhkt  wrote:
>
> >> Hi All
>
> >> Machine : AIX 5.3
> >> Python : 2.6.2
>
> > Yep! Check out the 'signal' module:
>
> >http://docs.python.org/3.3/library/signal.html
>
> Or, since you're working with 2.6.2:
>
> http://docs.python.org/2.6/library/signal.html
>
> ChrisA

Thank. will try.
-- 
http://mail.python.org/mailman/listinfo/python-list


Increase value in hash table

2013-01-22 Thread moonhkt
Hi Al

I have Data file have below

Data file
V1
V2
V3
V4
V4
V3

How to using count number of data ?

Output
V1 = 1
V2 = 1
V3 =2
V4 = 2



# Global Veriable
printque = {}
in def have below

printque[val] =  printque[val] + 1

I have below error
  File "xprintlogchk.py", line 78, in chklog
printque[val] =  printque[val] + 1
KeyError: 'nan'

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


Re: Increase value in hash table

2013-01-23 Thread moonhkt
On Jan 23, 3:34 pm, Chris Rebert  wrote:
> On Jan 22, 2013 11:31 PM, "moonhkt"  wrote:
>
>
>
>
>
>
>
>
>
>
>
> > Hi Al
>
> > I have Data file have below
>
> > Data file
> > V1
> > V2
> > V3
> > V4
> > V4
> > V3
>
> > How to using count number of data ?
>
> > Output
> > V1 = 1
> > V2 = 1
> > V3 =2
> > V4 = 2
>
> Construct a frequency table using collections.Counter:
>
> http://docs.python.org/2.7/library/collections.html#collections.Counter

What is problem for below ?
#!/usr/bin/env python
# Python hash {}
# Python Lists  []

global age
karry = "ER"
k1 = "EU"
age = {}
age[karry] = 3
age[k1] = 5

def ABC():
   global age
   global karry
   i = 0
   a = "A B"
   karry = a.split()
   age[karry[0]] += 1


ABC()
for key in age:
   print key,  age[key]



Result
ex_array.py
Traceback (most recent call last):
  File "ex_array.py", line 21, in 
ABC()
  File "ex_array.py", line 18, in ABC
age[karry[0]] += 1
KeyError: 'A'

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


Re: Increase value in hash table

2013-01-23 Thread moonhkt
On Jan 23, 11:33 pm, moonhk  wrote:
> Works.
>
>      prndev = line.split()
>          # print line
>          for key in prndev :
>              if key in 'lpr':
>                 val = prndev[5].replace("-P","")
>                 if val not in printque:
>                    printque[val] = 1
>                 else:
>                    printque[val] = printque[val] + 1
>              if key in "/dev/null":
>                  val='null'
>                  if val not in printque:
>                     printque[val] = 1
>                  else:
>                     printque[val] = printque[val] + 1
>
> On Wed, Jan 23, 2013 at 6:12 PM, Oscar Benjamin
>
>
>
>
>
>
>
>
>
>  wrote:
> > On 23 January 2013 07:26, moonhkt  wrote:
> >> Hi Al
>
> >> I have Data file have below
>
> >> Data file
> >> V1
> >> V2
> >> V3
> >> V4
> >> V4
> >> V3
>
> >> How to using count number of data ?
>
> >> Output
> >> V1 = 1
> >> V2 = 1
> >> V3 =2
> >> V4 = 2
>
> >> # Global Veriable
> >> printque = {}
> >> in def have below
>
> >> printque[val] =  printque[val] + 1
>
> >> I have below error
> >>   File "xprintlogchk.py", line 78, in chklog
> >>     printque[val] =  printque[val] + 1
> >> KeyError: 'nan'
>
> > You can't retrieve the value of printque[val] if you haven't yet added
> > an entry with the key val to the dict. Try this:
>
> > if val not in printque:
> >     printque[val] = 1
> > else:
> >     printque[val] = printque[val] + 1
>
> > Oscar
>
> --
> moonhkt
> GMT+8

Tried below works
  a = "A B"
   karry = a.split()
   age[karry[0]] = age.get(karry[0], 100) + 1
   age[karry[1]] = age.get(karry[1], 0) + 1
   age[karry[1]] = age.get(karry[1], 0) + 1


Result
A 101
B 2
-- 
http://mail.python.org/mailman/listinfo/python-list


create object base on text file

2013-01-25 Thread moonhkt
Hi All

Python 2.6.x on AIX

Data file

PrinterA
  print Production batch1
 xx
  print Production batch2
 xx
  print Production batch3
   xxx

PrinterB
 print Production batch4
   xxx
  print Production batch5
  


What to using python create object base on date file ? I know how to
read text file.

object["PrinterA"] have  batch1,  batch2,  batch3

object["PrinterB"] have  batch4, batch5

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


Split string data have ","

2013-01-29 Thread moonhkt
Hi All

Python 2.6.2 on AIX 5.3
How to using split o

>>> y = '"abc.p,zip.p",a,b'
>>> print y
"abc.p,zip.p",a,b
>>>

>>> k= y.split(",")
>>> print k[0]
"abc.p
>>>

Need Result, First element is
abc.p,zip.p
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Split string data have ","

2013-01-29 Thread moonhkt
On Jan 30, 1:08 am, Chris Rebert  wrote:
> On Jan 29, 2013 9:05 AM, "moonhkt"  wrote:
>
>
>
>
>
>
>
>
>
>
>
> > Hi All
>
> > Python 2.6.2 on AIX 5.3
> > How to using split o
>
> > >>> y = '"abc.p,zip.p",a,b'
> > >>> print y
> > "abc.p,zip.p",a,b
>
> > >>> k= y.split(",")
> > >>> print k[0]
> > "abc.p
>
> > Need Result, First element is
> > abc.p,zip.p
>
> Try the csv module or the shlex module.

Thank a lot, Using csv is good for me.
-- 
http://mail.python.org/mailman/listinfo/python-list


Read utf-8 file

2013-03-18 Thread moonhkt
File have China Made
中國 製

http://www.fileformat.info/info/unicode/char/4e2d/index.htm
UTF-16 (hex)0x4E2D (4e2d)
UTF-8 (hex) 0xE4 0xB8 0xAD (e4b8ad)


Read by od -cx utf_a.text
000   中  **  **  國  **  **  製  **  **  \n
e4b8ade59c8be8a3bd0a
012

Read by python, why python display as beow ?

中國製

u'\u4e2d\u570b\u88fd\n'  <--- Value 中國製
<-- UTF-8 value
u'\u4e2d' 中  CJK UNIFIED IDEOGRAPH-4E2D
u'\u570b' 國  CJK UNIFIED IDEOGRAPH-570B
u'\u88fd' 製  CJK UNIFIED IDEOGRAPH-88FD

import unicodedata
import codecs # UNICODE


file = codecs.open(options.filename, 'r','utf-8' )
try:
  for line in file:
 #print repr(line)
 #print "="
 print line.encode("utf")
 for keys in line.split(","):

   print repr(keys)  ," <--- Value" ,  keys.encode("utf") ,"<--
UTF-8 value"
   for key in keys:
 try:
name = unicodedata.name(unicode(key))
print "%-9s %-8s %-30s" % ( (repr(key)),
key.encode("utf") , name )


How to display
e4b8ad for 中 in python ?
-- 
http://mail.python.org/mailman/listinfo/python-list