rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » Python

第一次python编程-FTP、执行外部命令

文章分类:Python编程

所做的第一个python工作是这样的,首先需要到ftp上下载一部分加密资料,然后解码,取出一些需要的变量,然后计算,计算完毕以后插值,再放到指定的ftp上去,在这里python主要做些辅助工作,实现的都是类似shell的功能,对ftp的实现采用了两种办法,一种是调用python的ftplib,一种是用系统自己的ftp。

一、主控制ctr.py
Python代码
  • #!/usr/bin/env python   
  • #coding=utf-8   
  • import os   
  • cmd1="python /home/decode.py"  
  • cmd2="python /home/remerge.py"  
  • cmd3="python /home/flwtest.py"  
  • cmd4="python /home/ftpput.py"  
  • os.system(cmd1)   
  • os.system(cmd2)   
  • os.system(cmd3)   
  • os.system(cmd4)  
  • #!/usr/bin/env python
    #coding=utf-8
    import os
    cmd1="python /home/decode.py"
    cmd2="python /home/remerge.py"
    cmd3="python /home/flwtest.py"
    cmd4="python /home/ftpput.py"
    os.system(cmd1)
    os.system(cmd2)
    os.system(cmd3)
    os.system(cmd4)
    

     其实很简单,import os就类似c 的include,就是加一个头文件,后面就可以调用os里面的函数,这里os.system你给他一个字符串,这里我给的都是需要顺序执行的代码。主控制需要设置定时执行,因为在linux上用,所以用的cron。cron设置方法如下

    Cron代码
  • 打开etc/crontab   
  • 添加 30 8 * * * python /home/get.py   
  • 30 9 * * *  root  python /home/ctr.py   
  • 即可,数据文件可自动建立默认在home/datadir   
  • contab 添加后需要重启服务   
  • /sbin/service crond restart   
  • /sbin/service crond reload   
  • 需要服务器启动即加载的话需要改 etc/rc.d/rc.local   
  • 里面末尾加上/sbin/service crond start  
  • 打开etc/crontab
    添加 30 8 * * * python /home/get.py
    30 9 * * *  root  python /home/ctr.py
    即可,数据文件可自动建立默认在home/datadir
    contab 添加后需要重启服务
    /sbin/service crond restart
    /sbin/service crond reload
    需要服务器启动即加载的话需要改 etc/rc.d/rc.local
    里面末尾加上/sbin/service crond start

    我的程序是每天运行,get.py每天8点半执行,ctr.py每天9点半执行

    二、get.py是用来ftp获取数据文件的

    其实就是一个ftp脚本

    Python代码
  • #!/usr/bin/env python    
  • #coding=utf-8   
  • import ftplib   
  • import os   
  • import datetime   
  • #yestday计算昨天时间,给出格式化字符串   
  • fte=datetime.datetime.now()   
  • fte1=fte + datetime.timedelta(days = -1)   
  • print datetime.datetime.strftime( fte, '%Y%m%d')   
  • yest=datetime.datetime.strftime( fte1, '%Y%m%d')   
  • print yest   
  • #设置ftp目录   
  • dirn = '/home/DATA/2009032800/'    
  • dir1 = '/home/DATA/'+yest+'12'  
  • file1 = '2009032800000.grb1'    
  • # Define the local directory name to put data in   
  • ddir="/home/datadir"  
  • # If directory doesn't exist make it   
  • if not os.path.isdir(ddir):   
  •     os.mkdir(ddir)   
  • # Change the local directory to where you want to put the data   
  • os.chdir(ddir)   
  •   
  • # login to FTP这里设置地址用户名密码   
  • f=ftplib.FTP("0.0.0.0", "user", "password")   
  • print f.getwelcome()   
  • # change the remote directory   
  • f.cwd(dir1)   
  • f.dir()   
  • # define filename   
  • #循环拼需要ftp的文件名   
  • ii=0  
  • file0 = yest+'12'  
  • temp1=''  
  • while ii<=20:   
  •     temp1=12*ii   
  •     temp2=str(temp1)   
  •     if ii<9:   
  •         file1=file0+'0'+temp2+'.grb1'    
  •         if ii==0:file1=file0+'00'+temp2+'.grb1'    
  •     else:   
  •         file1=file0+temp2+'.grb1'       
  •     ii=ii+1        
  •     print ii        
  •     print "download "+file1    
  •     f.retrbinary("RETR %s" % file1, open(file1,"wb").write,1024)    
  •     print file1+"download ok"  
  •   
  • f.close()   
  • print yest+" data download end!"  
  • #!/usr/bin/env python 
    #coding=utf-8
    import ftplib
    import os
    import datetime
    #yestday计算昨天时间,给出格式化字符串
    fte=datetime.datetime.now()
    fte1=fte + datetime.timedelta(days = -1)
    print datetime.datetime.strftime( fte, '%Y%m%d')
    yest=datetime.datetime.strftime( fte1, '%Y%m%d')
    print yest
    #设置ftp目录
    dirn = '/home/DATA/2009032800/' 
    dir1 = '/home/DATA/'+yest+'12'
    file1 = '2009032800000.grb1' 
    # Define the local directory name to put data in
    ddir="/home/datadir"
    # If directory doesn't exist make it
    if not os.path.isdir(ddir):
        os.mkdir(ddir)
    # Change the local directory to where you want to put the data
    os.chdir(ddir)
    # login to FTP这里设置地址用户名密码
    f=ftplib.FTP("0.0.0.0", "user", "password")
    print f.getwelcome()
    # change the remote directory
    f.cwd(dir1)
    f.dir()
    # define filename
    #循环拼需要ftp的文件名
    ii=0
    file0 = yest+'12'
    temp1=''
    while ii<=20:
        temp1=12*ii
        temp2=str(temp1)
        if ii<9:
            file1=file0+'0'+temp2+'.grb1' 
            if ii==0:file1=file0+'00'+temp2+'.grb1' 
        else:
            file1=file0+temp2+'.grb1'    
        ii=ii+1     
        print ii	 
        print "download "+file1 
        f.retrbinary("RETR %s" % file1, open(file1,"wb").write,1024) 
        print file1+"download ok"
    f.close()
    print yest+" data download end!"
    

    中间执行的一些decode、merge与主控制类似,就是调用os.system执行我的fortran程序

    三、最后把数据ftp到指定服务器上

    这里采用的是系统的ftp与前面的不同

    Python代码
  • #!/usr/bin/env python   
  • #coding=utf-8   
  • import os   
  • os.chdir("/home/kkk/")   
  • a=os.listdir("/home/kkk/")   
  • h=open ("22.ftp","w")   
  • h.write("user kkk kkk"+ '\n')   
  • h.write("bin "+ '\n')     
  • h.write("cd /outdata/ "+ '\n')     
  •   
  • for row in a:   
  •   h.write("put ")     
  •   h.write(row+ '\n')   
  • #print a   
  •   
  • h.write("bye "+ '\n')     
  • cmd1="ftp -nv 1.1.1.1 <22.ftp"  
  • os.system(cmd1)   
  • cmd2="rm *"  
  • os.system(cmd2)  
  • 顶一下
    (0)
    踩一下
    (0)