python编程注意点几则
来源:互联网 发布:哭声剧情解析知乎 编辑:程序博客网 时间:2024/06/06 00:17
1. 如何实现HTTPS链接?
#function: https connection test example#input: user: user name# password: the user login password# host: the host name #output: none#return: the result description def user_login(hostname, user, password): conns = httplib.HTTPSConnection(hostname, 443) strMethod = '/secret/login/cs?password=' + password + '&username=' + user conns.request('GET', strMethod, headers = {host, "User-Agent": "User-Agent: Mozilla/4.0 (compatible; MS IE 6.0; EIS iPanel 2.0;(ziva))", "Accept-language":"zh-cn", "Accept-Encoding":"gzip, deflate", "Accept": "application/xml", "Connection": "keep-alive"}) res_s = conns.getresponse() status = res_s.status ret = res_s.read() print 'ret: ', ret if( status != 200 ): print 'status: ', status return '' else: return ret conns.close()
如果字符串有UTF-8中文字符,如何写入文件?
错误的写法:
#function: test file write#input: file_name: the file name# strWrite: the written string#output: none#return: nonedef test_file_write(file_name, strWrite) stream_file = file(file_name, 'w') stream_file.write(strWrite) stream_file.close()
正确的写法:
#function: test file write#input: file_name: the file name# strWrite: the written string#output: none#return: noneimport codecsdef test_file_write(file_name, strWrite) stream_file = codecs.open(file_name, 'w', 'UTF-8') stream_file.write(strWrite) stream_file.close()
3. HTTP请求中有特殊字符,如何发送HTTP请求?
#function: test http request when the request includes special characters#input: host: the host name# user: the user name# password: the user login password# verification: the verfication codes#output: none#return: none def test_http_connection(hostname, user, password, verification): conns = httplib.HTTPSConnection(hostname, 443) # error coding # strMethod = 'secret/login/cs?username=' + user + '&password=' + password + '&verify_code=' + verification strMethod = 'secret/login/cs?' + urllib.urlencode({'username':user, 'password':password, 'verify_code':verification}) print 'method: ', strMethod conns.request('GET', strMethod, headers = {"Host": hostname, "User-Agent": "User-Agent: Mozilla/4.0 (compatible; MS IE 6.0; EIS iPanel 2.0;(ziva))", "Accept-language":"zh-cn", "Accept-Encoding":"gzip, deflate", "Accept": "application/xml", "Connection": "keep-alive"}) res_s = conns.getresponse() status = res_s.status ret = res_s.read() print 'ret: ', ret conns.close()
- python编程注意点几则
- python 爬虫4 一些编程注意项目
- python 编程中要注意的事情
- Python和C混合编程时需注意内存泄漏
- Python编程中需要注意的一些事
- 【Python专题】 使用pycharm+pyqt5编程需要注意的事情
- Python编程中一定要注意的那些“坑”(一)
- python编程中要注意的几点
- Python 注意
- 编程注意
- 编程注意
- 编程注意
- 编程注意
- 【机器配置】python与C混合编程注意机器与平台的位数
- 非编程人学Python,要注意哪些隐秘的错误认知?
- Python编程一定要注意的那些“坑”(八):赋值运算符=
- 非编程人学Python,要注意哪些隐秘的错误认知?
- 注意python中的引用
- 文章4
- java改变图片的大小
- hibernate保存时出现乱码
- 【计算几何小结】
- Android-vold源码分析之卸载SD卡(9)
- python编程注意点几则
- 日期与字符串的转化
- 努努的2012元旦前夕
- 使用无Cookie的Session状态
- 浅谈MySQL的存储引擎之表类型存储
- JQUERY MOBILE 的 基本理解
- 下载著名音乐播放器AIMP3的正式版
- 在Android上实现图像颜色过滤与反转
- java相关乱码总结