5月 29

pexpect调用git中cd目录问题解决

当前需要python调用git命令自动执行pull操作。因为git还需要输入密码,所以使用到pexpect模块。
最初写法

>>> import pexpect
>>> p = pexpect.spawn('cd /home/simonzhang/test/&& git pull ')

但是pexpect不能使用cd命令,报错如下:
pexpect.ExceptionPexpect: The command was not found or was not executable: cd.

只能先切换到相应目录中,然后在执行。修改代码如下,执行成功。

>>> import pexpect
>>> import os
>>> os.chdir('/home/simonzhang/test/')
>>> p = pexpect.spawn('git pull')

还有一点学习。cd为shell自带的命令,当用which和whereis查找时都会报找不到,因为命令是按照系统的PATH变量设置路径进行搜索。用type cd则显示cd为shell buildin命令。pexpect调用时并不加载shell,导致了该问题。

4月 28

flask学习记录

flask学习记录尽量将学习的部分写到了一个文件中。
数据库交互部分还是用python-mysql。
主要是学习,不考虑交互,大并发部分。
详细结构在附件中。

#!/bin/env python
# -*- coding:utf-8 -*-
# Date:        2015-04-01
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
#                   _ooOoo_
#                  o8888888o
#                  88" . "88
#                  (| -_- |)
#                  O\  =  /O
#               ____/`---'\____
#             .'  \\|     |//  `.
#            /  \\|||  :  |||//  \
#           /  _||||| -:- |||||-  \
#           |   | \\\  -  /// |   |
#           | \_|  ''\---/''  |   |
#           \  .-\__  `-`  ___/-. /
#         ___`. .'  /--.--\  `. . __
#      ."" '<  `.___\_<|>_/___.'  >'"".
#     | | :  `- `.;`\ _ /`;.`/ - ` : | |
#     \  \ `-.   \_ __\ /__ _/   .-` /  /
#======`-.____`-.___\_____/___.-`____.-'======
#                   `=---='
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#  佛祖保佑 代码高性能 不宕机 无bug
### END INIT INFO
import os
from flask import Flask, url_for
from flask import request, render_template, redirect
from flask import session, escape
# 上传使用
from werkzeug import secure_filename
# 导入配置文件
from config import * 
# 还是使用python mysql获取数据
from mysqlrun import *

app = Flask(__name__)

# 过滤后缀
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

# 测试GET和POST,文件上传部分
# 测试GET的链接 http://127.0.0.1:5000/test?name=simonzhang.net
@app.route('/test', methods=['GET', 'POST'])
def test(name=None):
    if request.method == 'GET':
        if request.args.get('name',''):
            _name = request.args.get('name','')
        else:
            _name = 'hi'
        return render_template('test.html', name=_name)
    elif request.method == 'POST':
        file = request.files['upfile']
        # 上传了真实文件,并且后缀是通过的才保存
        if file and allowed_file(file.filename):
            # 此部分需要使用 from werkzeug import secure_filename
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(file.filename)))
        return render_template('test.html', name="ok")
    else:
        pass

# 测试cookie 当前还未完全成功
from flask import make_response
@app.route('/testcookie', methods=['GET'])
def testcookie():
    if request.method == 'GET':
        get_type = request.args.get('t','')
        get_value = request.args.get('v','')
        if get_type == 'r':
            _v = request.cookies.get('username')
            return 'user name : %s' % _v
        else:
            #resp = make_response(render_template('/'))
            request.set_cookie('username', get_value)
            return 'ok !!@'

# 错误页
@app.errorhandler(404)
def page_not_found(error):
    return render_template('404.html'), 404

# 判断是否登陆
@app.route('/')
def index():
    if 'username' in session:
        return 'Logged in as %s' % escape(session['username'])
    return 'You are not logged in'

# 登陆,简单的登陆页面,使用session
@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        session['username'] = request.form['username']
        return redirect(url_for('index'))
    return '''
        

''' # 注销 @app.route('/logout') def logout(): session.pop('username', None) return redirect(url_for('index')) if __name__ == '__main__': #app.debug = False app.debug = True # 上传部分 app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER app.config['MAX_CONTENT_LENGTH'] = UPLOAD_FILE_SIZE # 安全 app.secret_key = secret_key app.run(host = '0.0.0.0', port=500)

flask-study

4月 15

python 读取 xml

 
 
2 
提交成功
import xml.dom.minidom 

xml_context = ''' 
                  
                 2 
                 提交成功''' 
_xml = xml.dom.minidom.parseString(xml_context) 
_xml_code = _xml.getElementsByTagName('code') 
_code_v = int(_xml_code[0].childNodes[0].nodeValue) 
print _code_v

  说明,最后将值转为int只是为了后面的if判断方便。如果是xml比较大用这个还可以。
如果只是取单个数据,还不如直接正则或者find方便。

3月 04

php调用python

tophp.py

#!/bin/env python
# -*- coding:utf-8 -*-
# Filename:
# Revision: 
# Date:        205-03-04
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
### END INIT INFO
import sys

def test(ar):
    return "your key:%s" % ar

if __name__ == '__main__':
    ar = sys.argv[1]
    if ar:
       get_content = test(ar)
       print get_content

test_getpython.php

然后浏览器里面输入 http://localhost/test_getpython.php?key=test
浏览器会返回your key:test

学习页面:http://blog.163.com/darwin_zhang/blog/static/128488736201111842115199/