32位python的bug:os.system返回码一直为0

2016年9月26日 20:33

32位python在windows上调用命令行(os.system或subprocess.Popen)。执行后,如果返回码太大,python取得的返回值也是0。此时无法判断执行成功还是失败,这个是32位python的bug。
 
以时间同步命令w32tm位例子

在cmd上执行

C:\WINDOWS\system32>w32tm /resync
发生下列错误: 服务尚未启动。 (0x80070426)

C:\WINDOWS\system32>echo %errorlevel%
-2147023834

在64位python上执行

>>> os.system("w32tm /resync")
发生下列错误: 服务尚未启动。 (0x80070426)
-2147023834

在32位python上执行

>>> os.system("w32tm /resync")
发生下列错误: 服务尚未启动。 (0x80070426)
0
注意:此时命令执行错误的返回码也是0。
通常成功返回码才是0,这里执行错误,返回码却是也0。当要判断执行成功还失败时,这里便是个坑。
  • os.system文档
https://docs.python.org/2/library/os.html
 

Tags: popen system python
评论(0) 阅读(774)

cmd切换为administrator用户

2016年9月26日 19:46

runas /noprofile /user:Administrator cmd
  • runas         允许用户用其它权限运行指定的工具和程序
  • /noprofile    指定不应该加载用户的配置文件。
  • /user:UserAccountName  指定在其下运行程序的用户帐户的名称
 

Tags: windows DOS runas
评论(0) 阅读(805)

git status 显示乱码

2016年5月27日 05:22

  • 查看修改历史,出现下面"乱码"
修改:     "data/python\345\255\246\344\271\240\350\265\204\346\226\231.wiki"
  • 设置
git config core.quotepath false
显示正常
 

评论(0) 阅读(931)

apscheduler提示maximum错误

2015年12月15日 20:59

起因

在tornado中用apscheduler实现计划任务,出现错误提示
 
2015-12-04 19:10:22,227 - apscheduler.scheduler - WARNING - Execution of job "TaskHandle.progress_job (trigger: date[2015-12-04 19:10:22 CST], next run at: 2015-12-04 19:10:22 CST)" skipped: maximum number of running instances reached (1)

分析

缺乏资料,所以根据提示,分析apscheduler源码
  • 记录异常位置
在源码目录下,搜索关键字maxinum,找到记录异常的位置
#apscheduler/schedulers/base.py
if run_times:
    try:
        executor.submit_job(job, run_times)
    except MaxInstancesReachedError:
        self._logger.warning(
            'Execution of job "%s" skipped: maximum number of running instances reached (%d)',
            job, job.max_instances)
    except:
        self._logger.exception('Error submitting job "%s" to executor "%s"', job, job.executor)
  • 异常抛出位置
 
继续看submit_job函数,找到异常抛出位置
#apscheduler/executors/base.py
def submit_job(self, job, run_times):
    """
    Submits job for execution.

    :param Job job: job to execute
    :param list[datetime] run_times: list of datetimes specifying when the job should have been run
    :raises MaxInstancesReachedError: if the maximum number of allowed instances for this job has been reached
    """

    assert self._lock is not None, 'This executor has not been started yet'
    with self._lock:
        if self._instances[job.id] >= job.max_instances:
            raise MaxInstancesReachedError(job)

        self._do_submit_job(job, run_times)
        self._instances[job.id] += 1
  • _instances变量作用
在submit_job(提交任务)时加1,在_run_job_success(任务运行成功)时减1。 当self._instances[job.id]大于job.max_instances抛出异常。
max_instances默认值为1,它表示id相同的任务实例数。

解决

通过设置max_instances参数
sched.add_job(child_job, max_instances=10, trigger=DateTrigger(), id="123")

重现脚本

import time
import tornado.ioloop
from apscheduler.triggers.date import DateTrigger
from apscheduler.schedulers.tornado import TornadoScheduler

sched = TornadoScheduler()

def child_job():
    print "start"
    time.sleep(60)
    print "end"

def main_job():
    sched.add_job(child_job, trigger=DateTrigger(), id="123")

sched.add_job(main_job, 'interval', seconds=5)

sched.start()
tornado.ioloop.IOLoop.instance().start()
  • 输出
job_id: 7279209ab6c2498698f2117bb97e18a1, instances: 0, max_instances: 1
job_id: 123, instances: 0, max_instances: 1
start
job_id: 7279209ab6c2498698f2117bb97e18a1, instances: 0, max_instances: 1
job_id: 123, instances: 1, max_instances: 1
WARNING:apscheduler.scheduler:Execution of job "child_job (trigger: date[2015-12-07 15:27:11 CST], next run at: 2015-12-07 15:27:11 CST)" skipped: maximum number of running instances reached (1)
job_id: 7279209ab6c2498698f2117bb97e18a1, instances: 0, max_instances: 1
 

Tags: tornado;apscheduler
评论(0) 阅读(2283)

python paramiko实现ssh远程登录

2015年11月13日 21:03

  • 介绍
paramiko是python的一个模块,遵循SSH2协议。 paramiko的功能:1、通过ssh执行命令; 2、通过ssh传输文件。
  • 示例
import paramiko

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname="xxx.xxx.xxx.xxx", port=22, username="xxx", password="xxx")
stdin, stdout, stderr = ssh.exec_command("vmstat 1 1")
content = stdout.readlines()
for r in content:
    print r
ssh.close()
 

Tags: paramiko
评论(0) 阅读(1798)

在windows中使用vagrant

2015年11月13日 21:02

  • 介绍
vagrant封装linux开发环境,可以统一团队成员的开发环境。封装后,可以linux/windows/mac上运行.
  • 环境
以win8安装vagrant为例子。vagrant需要virtualbox提供虚拟化支持,因此先安装virtualbox。
  • 安装
https://www.vagrantup.com/downloads.html
  • vagrant镜像
vagrant镜像下载地址
 
http://www.vagrantbox.es/
 
以centos-7.0为例子,下载地址
 
https://github.com/tommy-muehle/puppet-vagrant-boxes/releases/download/1.1.0/centos-7.0-x86_64.box
  • 新建目录
D:\vagrant\centos
 
下载后的centos-7.0-x86_64.box放到此目录
  • 添加box
D:\vagrant\centos>vagrant box add --name centos7 .\centos-7.0-x86_64.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos7' (v0) for provider:
    box: Unpacking necessary files from: file://D:/vagrant/centos/centos-7.0-x86
_64.box
    box: Progress: 100% (Rate: 48.1M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'centos7' (v0) for 'virtualbox'!
  • 查看box列表
D:\vagrant\centos>vagrant box list
centos7            (virtualbox, 0)
  • 初始化工作目录
D:\vagrant\centos>vagrant init centos7
  • 启动
D:\vagrant\centos>vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 => 2222 (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2222
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: Warning: Connection timeout. Retrying...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
==> default: Mounting shared folders...
    default: /vagrant => D:/vagrant/centos
==> default: Machine already provisioned. Run `vagrant provision` or use the `--
provision`
==> default: flag to force provisioning. Provisioners marked to run always will
still run.
  • 安装ssh工具
 
启动之后,登录系统需要ssh,windows没有ssh,可以安装copssh。安装后,再将ssh.exe路径配置到环境变量中。
 
https://www.itefix.net/content/copssh-free-edition
  • 登录
D:\vagrant\centos>vagrant ssh
cygwin warning:
  MS-DOS style path detected: D:/vagrant/centos/.vagrant/machines/default/virtua
lbox/private_key
  Preferred POSIX equivalent is: /cygdrive/d/vagrant/centos/.vagrant/machines/de
fault/virtualbox/private_key
  CYGWIN environment variable option "nodosfilewarning" turns off this warning.
  Consult the user's guide for more details about POSIX paths:
    http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
Last login: Thu Nov  5 03:10:24 2015 from 10.0.2.2
Welcome to your Vagrant-built virtual machine.
[vagrant@localhost ~]$ 
使用其它工具登录,初始账号是vagrant/vagrant
  • 关闭
D:\vagrant\centos>vagrant halt
==> default: Attempting graceful shutdown of VM...
  • vagrant命令
vagrant up  启动
vagrant halt 普通关闭
vagrant ssh 连接
vagrant suspend 休眠
vagrant reload  重启
vagrant destroy 删除
vagrant box add --name box_name /path/of/box/file #添加本地box
 

Tags: vagrant;virtualbox
评论(0) 阅读(1531)

salt学习笔记

2015年11月13日 20:56

salt使用主从结构配置、管理、监控远程机器。配置简单,功能强大,是很好的运维管理工具。
  • 准备环境
使用vmware,准备两台虚拟机192.168.8.10和192.168.8.20,192.168.8.10作为master,192.168.820作为minion.
  • master
yum install salt-master   //master
  • 配置master
 
打开/etc/salt/master
 
interface: 192.168.8.10
  • 启动master
service salt-master start
  • minion
yum install salt-minion   //minion
  • 配置minion
 
打开/etc/salt/master
master: 192.168.8.10
  • 启动minion
service salt-minion start
  • 查看minion列表
 
在master上运行
➜  ~  salt-key -L
Accepted Keys:
192.168.124.1
Denied Keys:
Unaccepted Keys:
Rejected Keys:
  • 设置master接受所有minion
salt-key -A
  • 测试minion
salt '*' test.ping
  • 运行命令
salt '192.168.8.20' cmd.run 'df -Th'
  • 管理master与minion关系
salt-key -L 查看key列表
salt-key -D 删除所有key
salt-key -d key 删除单个key
salt-key -A 接受所有key
salt-key -a key 接受单个key
 

Tags: salt;服务器管理
评论(0) 阅读(1367)

去掉ssh初次登录询问yes/no

2015年11月11日 03:19

ssh初次登录某个主机时,出现如下提示
Are you sure you want to continue connecting (yes/no)?
  • 如何去掉?
打开/etc/ssh/ssh_config,修改以下配置项
#  StrictHostKeyChecking ask
   StrictHostKeyChecking no
 

评论(0) 阅读(1447)

calmari日志提示OperationalError Permission denied

2015年11月06日 03:40

问题

在fedora22系统中部署calmari,使用calamari-ctl initialize启动calamari后,访问浏览器,浏览器提示500。查看日志文件/var/log/calmari/httpd_error.log,得到以下内容。
[Tue Nov 03 19:42:01.210895 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168] mod_wsgi (pid=8586): Exception occurred processing WSGI script '/opt/calamari/conf/calamari.wsgi'.
[Tue Nov 03 19:42:01.210915 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168] Traceback (most recent call last):
[Tue Nov 03 19:42:01.210931 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
[Tue Nov 03 19:42:01.211007 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     response = self.get_response(request)
[Tue Nov 03 19:42:01.211018 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 178, in get_response
[Tue Nov 03 19:42:01.211032 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
[Tue Nov 03 19:42:01.211040 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 220, in handle_uncaught_exception
[Tue Nov 03 19:42:01.211050 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     if resolver.urlconf_module is None:
[Tue Nov 03 19:42:01.211056 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 342, in urlconf_module
[Tue Nov 03 19:42:01.211066 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     self._urlconf_module = import_module(self.urlconf_name)
[Tue Nov 03 19:42:01.211080 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
[Tue Nov 03 19:42:01.211091 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     __import__(name)
[Tue Nov 03 19:42:01.211097 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/calamari_web-0.1-py2.7.egg/calamari_web/urls.py", line 20, in <module>
[Tue Nov 03 19:42:01.211109 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     url(r'^api/v1/', include('calamari_rest.urls.v1')),
[Tue Nov 03 19:42:01.211115 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 25, in include
[Tue Nov 03 19:42:01.211125 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     urlconf_module = import_module(urlconf_module)
[Tue Nov 03 19:42:01.211131 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
[Tue Nov 03 19:42:01.211139 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     __import__(name)
[Tue Nov 03 19:42:01.211145 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/calamari_rest_api-0.1-py2.7.egg/calamari_rest/urls/v1.py", line 3, in <module>
[Tue Nov 03 19:42:01.211156 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     import calamari_rest.views.v1
[Tue Nov 03 19:42:01.211162 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/calamari_rest_api-0.1-py2.7.egg/calamari_rest/views/v1.py", line 33, in <module>
[Tue Nov 03 19:42:01.211171 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     from graphite.render.datalib import fetchData
[Tue Nov 03 19:42:01.211177 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/graphite/render/datalib.py", line 20, in <module>
[Tue Nov 03 19:42:01.211187 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     from graphite.storage import STORE, LOCAL_STORE
[Tue Nov 03 19:42:01.211192 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/graphite/storage.py", line 7, in <module>
[Tue Nov 03 19:42:01.211201 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     from graphite.remote_storage import RemoteStore
[Tue Nov 03 19:42:01.211207 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/graphite/remote_storage.py", line 8, in <module>
[Tue Nov 03 19:42:01.211216 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     from graphite.util import unpickle
[Tue Nov 03 19:42:01.211221 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/graphite/util.py", line 71, in <module>
[Tue Nov 03 19:42:01.211230 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     defaultUser = User.objects.get(username='default')
[Tue Nov 03 19:42:01.211235 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/db/models/manager.py", line 143, in get
[Tue Nov 03 19:42:01.211245 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     return self.get_query_set().get(*args, **kwargs)
[Tue Nov 03 19:42:01.211251 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/db/models/query.py", line 382, in get
[Tue Nov 03 19:42:01.211259 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     num = len(clone)
[Tue Nov 03 19:42:01.211265 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/db/models/query.py", line 90, in __len__
[Tue Nov 03 19:42:01.211272 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     self._result_cache = list(self.iterator())
[Tue Nov 03 19:42:01.211282 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/db/models/query.py", line 301, in iterator
[Tue Nov 03 19:42:01.211290 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     for row in compiler.results_iter():
[Tue Nov 03 19:42:01.211295 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 775, in results_iter
[Tue Nov 03 19:42:01.211305 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     for rows in self.execute_sql(MULTI):
[Tue Nov 03 19:42:01.211310 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 839, in execute_sql
[Tue Nov 03 19:42:01.211318 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     cursor = self.connection.cursor()
[Tue Nov 03 19:42:01.211324 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/db/backends/__init__.py", line 326, in cursor
[Tue Nov 03 19:42:01.211333 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     cursor = util.CursorWrapper(self._cursor(), self)
[Tue Nov 03 19:42:01.211339 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 182, in _cursor
[Tue Nov 03 19:42:01.211348 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     self.connection = Database.connect(**conn_params)
[Tue Nov 03 19:42:01.211354 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]   File "/opt/calamari/venv/lib/python2.7/site-packages/psycopg2/__init__.py", line 167, in connect
[Tue Nov 03 19:42:01.211363 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168]     conn = _connect(dsn, connection_factory=connection_factory, async=async)
[Tue Nov 03 19:42:01.211376 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168] OperationalError: could not connect to server: Permission denied
[Tue Nov 03 19:42:01.211380 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168] \tIs the server running on host "localhost" (127.0.0.1) and accepting
[Tue Nov 03 19:42:01.211383 2015] [wsgi:error] [pid 8586] [remote 127.0.0.1:168] \tTCP/IP connections on port 5432?

解决

原因可能是postgresql自身访问限制、用户权限问题,检查后均被排除。查询资料得知,访问权限被selinux限制了。
  • 第一种方法是设置selinux去掉访问限制
sudo setsebool -P httpd_can_network_connect_db on
  • 第二种方法是关闭selinux
编辑/etc/selinux/config,将参数配为禁用,再重启系统。
SELINUX=disabled

相关

getsebool -a 查看selinux控制项
 

 

Tags: ceph;django
评论(0) 阅读(1272)

gettext国际化用法示例

2015年9月09日 16:36

  • 安装gettext
sudo yum install gettext
  • gettext工具
gettext: 进行translate。
 
xgettext: 从程序中抽取调用gettext进行本地化的字符串,生成一份.po结尾的配置文件。
 
msgfmt: 将配置好的本地化配置文件进行转换成gettext使用的格式。
  • 准备demo.py
#encoding=utf-8
import gettext

# demo对应mo文件名,locale为locale目录地址,zh_CN为locale目录下目录名
zh = gettext.translation("demo", "locale", languages=["zh_CN"])
# 激活_()
zh.install(True)
print _("hello world")
  • 生成po文件
 
从程序文件中抽取,使用gettext的字符串,生成po文件
xgettext -L python -o zh_CN.po demo.py
  • 编辑po文件
...
#修改编码为utf-8
"Content-Type: text/plain; charset=utf-8\n"
...

#加上翻译
#: demo.py:6
msgid "hello world"
msgstr "你好,世界"
  • 创建locale目录
mkdir -p locale/zh_CN/LC_MESSAGES
 
* 编译po文件
msgfmt -o ~/locale/zh_CN/LC_MESSAGES/demo.mo zh_CN.po
  • 查看结果
➜  ~  python demo.py
你好,世界
 

Tags: 国际化
评论(0) 阅读(1490)