技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> Linux --> 【总结】美化bash,python的soap client,python获取系统编码函数

【总结】美化bash,python的soap client,python获取系统编码函数

浏览:4132次  出处信息

一.美化你的bash
每次在进入一个很长的目录的时候,光标都会变得很靠右,很丑陋:

1

今天同事给了一段配置:

export PS1="\[\e[36;1m\]\u\[\e[0m\]@\[\e[33;1m\]\h\[\e[0m\]:\[\e[31;1m\]\w\[\e[0m\]\n\$ "

写入.profile后,会变成如下的样子,效果不错,哈哈:

2

二.在python中使用soap -- suds
今天需要通过soap来给RTX的其他用户发消息,于是研究了一下python中的soap包。
先是发现了SOAPpy,包括Dive into Python都推荐了这个包,不过经过我的试用之后,发现打死都显示不了任何数据,于是我搜啊。。终于在某个不起眼的角落看到了这样一段话:

Unfortunately, at the moment, I don't think there is a "best" Python SOAP library. Each of the mainstream ones available has its own pros and cons.
Older libraries:
SOAPy: Was the "best," but no longer maintained. Does not work on Python 2.5+
ZSI: Very painful to use, and development is slow. Has a module called "SOAPpy", which is different than SOAPy (above).
"Newer" libraries:
SUDS: Very Pythonic, and easy to create WSDL-consuming SOAP clients. Creating SOAP servers is a little bit more difficult.
soaplib: Creating servers is easy, creating clients a little bit more challenging.

OK,那就用SUDS吧~
下载链接如下:
https://fedorahosted.org/suds/
测试代码如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from suds.client import Client
def SendRtx(target,title,content):
    url = "http://ws.oa.com/messageservice.asmx?wsdl"
    client = Client(url)
    client.service.SendRTX(
            sender = 'dantezhu',
            receiver = target,
            title = title,
            msgInfo = content,
            messageType = 0
            )
print SendRtx('dantezhu','我'.decode('utf-8'),'ss')

由于suds要求中文字符都要经过decode,所以代码中会将中文decode。

不过说来奇怪,这段代码在linux可以正常运行,但是在windows下就一直报错了。。

三.Python获取系统编码参数的几个函数

系统的缺省编码(一般就是ascii):sys.getdefaultencoding()
系统当前的编码:locale.getdefaultlocale()
系统代码中临时被更改的编码(通过locale.setlocale(locale.LC_ALL,"zh_CN.UTF-8")):locale.getlocale()
文件系统的编码:sys.getfilesystemencoding()
终端的输入编码:sys.stdin.encoding
终端的输出编码:sys.stdout.encoding
代码的缺省编码:文件头上# -*- coding: utf-8 -*-

新建一个py文件,代码如下:

import sys,locale
print sys.getdefaultencoding()
print locale.getdefaultlocale()
print sys.getfilesystemencoding()
print sys.stdin.encoding
print sys.stdout.encoding
try:
    locale.setlocale(locale.LC_ALL,"zh_CN.GB2312")
except locale.Error,e:
    print e
print locale.getlocale()
print locale.getdefaultlocale()

在我的windows下的输出是:

ascii
('zh_CN', 'cp936')
mbcs
cp936
None
unsupported locale setting
(None, None)
('zh_CN', 'cp936')

在linux下输出是:

ascii                                                                                              
('en_GB', 'UTF8')
UTF-8
UTF-8
None
('zh_CN', 'gb2312')
('en_GB', 'UTF8')

OK,就是一些常用的东西总结了一下~

建议继续学习:

  1. 配置Nginx+uwsgi更方便地部署python应用    (阅读:105183)
  2. 如何成为Python高手    (阅读:53118)
  3. python实现自动登录discuz论坛    (阅读:31405)
  4. Bash的模式和配置文件加载    (阅读:23128)
  5. python编程细节──遍历dict的两种方法比较    (阅读:18822)
  6. 每个程序员都应该学习使用Python或Ruby    (阅读:16085)
  7. 使用python爬虫抓站的一些技巧总结:进阶篇    (阅读:11901)
  8. 30分钟3300%性能提升――python+memcached网页优化小记    (阅读:11839)
  9. 我的PHP,Python和Ruby之路    (阅读:11685)
  10. Python处理MP3的歌词和图片    (阅读:8122)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1