IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

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

Vimer 2010-09-13 19:50:45 累计浏览 5,421 次
本机暂存

一.美化你的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. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

  1. 用Hyer来进行网站的抓取 (累计阅读 158,252)
  2. 配置Nginx+uwsgi更方便地部署python应用 (累计阅读 107,167)
  3. 程序员技术练级攻略 (累计阅读 35,472)
  4. python实现自动登录discuz论坛 (累计阅读 32,834)
  5. Bash的模式和配置文件加载 (累计阅读 24,410)
  6. python编程细节──遍历dict的两种方法比较 (累计阅读 20,371)
  7. 每个程序员都应该学习使用Python或Ruby (累计阅读 17,919)
  8. Chrome和goagent的配置方法,你懂的 (累计阅读 16,843)
  9. 30分钟3300%性能提升――python+memcached网页优化小记 (累计阅读 13,742)
  10. 使用python爬虫抓站的一些技巧总结:进阶篇 (累计阅读 13,302)