Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 38 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Baidu Push 服务器端SDK Python版

==============
吃水不忘挖井人,该sdk是在@luvchh基础上修改核心http请求方式来的,98%的代码是@luvchh完成的
**第一版:**
由[@luvchh](http://weibo.com/luvchh) 提供,并开放在github上:
github地址:<https://github.com/Argger/pusher_python_sdk>
**第二版:**
由[@搞基宫陈尸](http://www.weibo.com/u/2255232584) 修改,
github地址:<https://github.com/blacklaw0/pusher_python_sdk>
- - -
Python SDK总体介绍:
将百度Push服务端的所有操作封装成一个类Channel,通过对该类的简单初始化,即可调用其内部的各种方法,使用百度Push服务。
Channel提供的方法和服务端API对应,是对服务端REST API的封装,REST API请参考:http://developer.baidu.com/wiki/index.php?title=docs/cplat/push/api/list
Expand All @@ -9,40 +16,43 @@ Channel提供的方法和服务端API对应,是对服务端REST API的封装

工具组成:
Python SDK工具包主要由以下部分组成:
Channel.py -- Python_SDK 脚本,包含对外提供的所有接口
lib -- 使用到的一些基础公用文件
sample/sample.python -- 展示如何使用 Python_SDK 的 demo 文件
* Channel.py -- Python_SDK 脚本,包含对外提供的所有接口
* lib -- 使用到的一些基础公用文件
* sample/sample.python -- 展示如何使用 Python_SDK 的 demo 文件

SDK 依赖于以下组件:
 pycurl
python
* urllib,urllib2
* python


一般规则
所有函数的参数和返回值中如果有中文,必须是UTF-8编码;
不需要对函数参数进行urlencode。
* 所有函数的参数和返回值中如果有中文,必须是UTF-8编码;
* 不需要对函数参数进行urlencode。
错误码集合
如果用户在调用SDK时发生错误,那么错误分成两大类:
 与服务交互失败产生的错误,比如:SDK参数不完整、网络错误、服务器的返回不是正确的json包导致无法解析等,这类错误的错误码位于1-100区间,具体如下:
服务器交互失败错误
错误码 错误信息
1 python sdk error
2 Python sdk init error
3 lack param
4 http status is error, and the body returned is not a json string
5 http status is ok, but the body returned is not a json string
* 与服务交互失败产生的错误,比如:SDK参数不完整、网络错误、服务器的返回不是正确的json包导致无法解析等,这类错误的错误码位于1-100区间,具体如下:
服务器交互失败错误

错误码 错误信息


>1 python sdk error
>2 Python sdk init error
>3 lack param
>4 http status is error, and the body returned is not a json string
>5 http status is ok, but the body returned is not a json string

与服务器交互成功,但服务器返回了非200的HTTP状态,比如用户权限错误、重复绑定等,具体如下:
***与服务器交互成功,但服务器返回了非200的HTTP状态,比如用户权限错误、重复绑定等,具体如下:***
HTTP状态错误
错误码 错误信息
30600 Internal Server Error
30601 Method Not Allowed
30602 Request Params Not Valid
30603 Authentication Failed
30604 Quota Use Up Payment Required
30605 Data Required Not Found
30606 Request Time Expires Timeout
30607 Channel Token Timeout
30608 Bind Relation Not Found
30609 Bind Number Too Many
30610 Duplicate Operation
>30600 Internal Server Error
>30601 Method Not Allowed
>30602 Request Params Not Valid
>30603 Authentication Failed
>30604 Quota Use Up Payment Required
>30605 Data Required Not Found
>30606 Request Time Expires Timeout
>30607 Channel Token Timeout
>30608 Bind Relation Not Found
>30609 Bind Number Too Many
>30610 Duplicate Operation
16 changes: 12 additions & 4 deletions python_sdk/Channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
# @copyright Copyright (c) 2012-2020 百度在线网络技术(北京)有限公司
# @version 1.0.0
###

'''
@author: blacklaw
@version: 1.1.0
@email: blacklaw00@gmail.com
@date: 2013-06-08
@description: 由于百度把pycurl给限制掉了,老版的不能用,现在改为urllib2实现
'''


class Channel(object):
Expand Down Expand Up @@ -481,7 +487,10 @@ def _baseControl(self, opt):

if(isinstance(self._curlOpts, dict)):
request.set_curlopts(self._curlOpts)

'''
@blacklaw
bae 把pycurl取消了,现在必须换成urllib
'''
request.handle_request()
return ResponseCore(request.get_response_header(),
request.get_response_body(),
Expand Down Expand Up @@ -543,9 +552,8 @@ def _mergeArgs(self, arrNeed, tmpArgs):


def _channelExceptionHandler(self, ex):
print ex.error_msg, self._arrayErrorMap[ex.error_code]
print ex.error_msg, ex.error_code, 'self._arrayErrorMap[ex.error_code]'





57 changes: 40 additions & 17 deletions python_sdk/lib/RequestCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
# _*_ coding: UTF-8 _*_

###
# 本文件百度云服务PHP版本SDK的公共网络交互功能
#
# @author 百度移动.云事业部
# @copyright Copyright (c) 2012-2020 百度在线网络技术(北京)有限公司
# @version 1.0.0
# @package
##

# 百度云推送PUSH服务 Python SDK
#
# 本文件提供百度云推动PUSH的Python版本SDK
#
# @author 百度云平台部
# @copyright Copyright (c) 2012-2020 百度在线网络技术(北京)有限公司
# @version 1.0.0
###
'''
@author: blacklaw
@version: 1.1.0
@email: blacklaw00@gmail.com
@date: 2013-06-08
@description: 由于百度把pycurl给限制掉了,老版的不能用,现在改为urllib2实现
'''

import urlparse

import pycurl

import StringIO

import urllib, urllib2

class RequestCore(object):
"""封装curl,提供网络交互功能,组网络请求包,并保存返回结果"""
Expand Down Expand Up @@ -87,11 +91,28 @@ def set_curlopts(self, curlopts):

def set_proxy(self, proxy):
self.proxy = urlparse.urlparse(proxy)


'''
bae 把pycurl 给取消了,我现在改成了urllib实现
'''
def handle_request(self):
url = self.request_url
headers = dict()
headers['HTTP_REFERER'] = self.request_url
request = urllib2.Request(url, self.request_body, headers)
f = urllib2.urlopen(request)
self.response_code = f.getcode()
self.response_headers = f.info()
self.response_body = f.read()

'''
以前使用的是Curl方法,baidu把它禁用了
def handle_request_curl(self):
print '\n\n',dir(self),'\n\n'
curl_handle = pycurl.Curl()
# set default options.
curl_handle.setopt(pycurl.URL, self.request_url)
url = 'http://127.0.0.1/gethead.php'
curl_handle.setopt(pycurl.URL, url)#self.request_url)
curl_handle.setopt(pycurl.REFERER, self.request_url)
curl_handle.setopt(pycurl.USERAGENT, self.useragent)
curl_handle.setopt(pycurl.TIMEOUT, 5184000)
Expand All @@ -104,9 +125,11 @@ def handle_request(self):
tmplist = list()
for(key, value) in self.request_headers.items():
tmplist.append(key + ':' + value)
print 'request keylist',dir(tmplist)
curl_handle.setopt(pycurl.HTTPHEADER, tmplist)
#目前只需支持POST
curl_handle.setopt(pycurl.HTTPPROXYTUNNEL, 1)
print '\n\n*************************\nrequest.body',self.request_body,'\n******************************\n'
curl_handle.setopt(pycurl.POSTFIELDS, self.request_body)

response = StringIO.StringIO()
Expand All @@ -118,11 +141,10 @@ def handle_request(self):
resp_str = response.getvalue()
self.response_headers = resp_str[0 : header_size]
self.response_body = resp_str[header_size : ]

print self.response_body
response.close()
curl_handle.close()


'''
def get_response_header(self, header = None):
if(header is not None):
return self.response_headers[header]
Expand Down Expand Up @@ -156,3 +178,4 @@ def isOK(self, codes = None):