本文整理汇总了Python中werobot.utils.to_binary函数的典型用法代码示例。如果您正苦于以下问题:Python to_binary函数的具体用法?Python to_binary怎么用?Python to_binary使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了to_binary函数的18个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_to_binary
def test_to_binary():
assert to_binary(6) == six.binary_type(6)
assert to_binary(b"aa") == b"aa"
assert to_binary("cc") == b"cc"
if six.PY2:
assert to_binary(u"喵") == "喵"
assert to_binary("喵") == "喵"
开发者ID:adam139,项目名称:WeRobot,代码行数:7,代码来源:test_utils.py
示例2: wish_read
def wish_read(message, session):
token = get_token(message, session)
if (None == token):
return "输入'豆瓣'完成授权后回到微信"
client_wechat.auth_with_token(token)
if get_state(session) == 'booklist' or get_state(session) == 'wishread':
bookid = session.get(message.content, 0)
if 0 == bookid:
return "输入有误,请重新输入"
else:
try:
client_wechat.book.collection(bookid)
except:
return "你收藏过这本书啦!"
else:
return "设置想读成功!"
set_state(session, 'wishread')
elif get_state(session) == 'dnlist':
dnstr = message.content
tedstr = ted_kv.get(to_binary(TED_POPULAR))
retstr = ''
if None != tedstr:
speaker_list = json.loads(tedstr)
dnid = int(dnstr)
if dnid >= len(speaker_list):
return '输入有误,请重新输入'
# change id to keyward
message.content = speaker_list[dnid]
开发者ID:Gitstring,项目名称:niubility,代码行数:28,代码来源:robot.py
示例3: get_reply
def get_reply(self, message):
"""
根据 message 的内容获取 Reply 对象。
:param message: 要处理的 message
:return: 获取的 Reply 对象
"""
session_storage = self.session_storage
id = None
session = None
if session_storage and hasattr(message, "source"):
id = to_binary(message.source)
session = session_storage[id]
handlers = self.get_handlers(message.type)
try:
for handler, args_count in handlers:
args = [message, session][:args_count]
reply = handler(*args)
if session_storage and id:
session_storage[id] = session
if reply:
return process_function_reply(reply, message=message)
except:
self.logger.exception("Catch an exception")
开发者ID:bug3306,项目名称:WeRoBot,代码行数:26,代码来源:robot.py
示例4: __init__
def __init__(self, key):
key = to_binary(key)
self.cipher = Cipher(
algorithms.AES(key),
modes.CBC(key[:16]),
backend=default_backend()
)
开发者ID:bug3306,项目名称:WeRoBot,代码行数:7,代码来源:__init__.py
示例5: get_reply
def get_reply(self, message):
"""
Return the raw xml reply for the given message.
"""
# 读取session
session_storage = self.config["SESSION_STORAGE"]
id = None
session = None
if session_storage and hasattr(message, "source"):
id = to_binary(message.source)
session = session_storage[id]
# 获取处理该信息的所有handler
handlers = self.get_handlers(message.type)
try:
for handler, args_count in handlers:
# 从list[...]中取args_count个构成新list
args = [message, session][:args_count]
reply = handler(*args)
if session_storage and id:
session_storage[id] = session
if reply:
return reply
except:
self.logger.warning("Catch an exception", exc_info=True)
开发者ID:binss,项目名称:werobot-bin,代码行数:27,代码来源:robot.py
示例6: encode
def encode(text):
# 计算需要填充的位数
amount_to_pad = _BLOCK_SIZE - (len(text) % _BLOCK_SIZE)
if not amount_to_pad:
amount_to_pad = _BLOCK_SIZE
# 获得补位所用的字符
pad = chr(amount_to_pad)
return text + to_binary(pad * amount_to_pad)
开发者ID:adam139,项目名称:WeRobot,代码行数:8,代码来源:pkcs7.py
示例7: get_token
def get_token(message, session):
guid = message.source
userstr = wechat_kv.get(to_binary(guid))
if None != userstr:
user = json.loads(userstr)
uid = user['uid']
token = user['token']
return token
return None
开发者ID:csufuyi,项目名称:moviemate,代码行数:9,代码来源:db.py
示例8: test_prpcrypto
def test_prpcrypto():
key = "ReUrr0NKeHkppBQq"
assert len(key) == 16
crypto = PrpCrypto(key)
text = generate_token(32)
app_id = generate_token(32)
assert crypto.decrypt(crypto.encrypt(text, app_id),
app_id) == to_binary(text)
开发者ID:whtsky,项目名称:WeRoBot,代码行数:10,代码来源:test_crypto.py
示例9: speaker
def speaker(message, session):
tedstr = ted_kv.get(to_binary(TED_POPULAR))
retstr = ''
if None != tedstr:
speaker_list = json.loads(tedstr)
if 0 == len(speaker_list):
return '大牛列表暂时为空'
for index in range(len(speaker_list)):
retstr += '[' + str(index) + ']. ' + speaker_list[index] + '\n'
retstr += u'\n输入序号可以查询大牛著作'
set_state(session, 'dnlist')
return retstr
return "大牛列表暂时还拉不到哦"
开发者ID:Gitstring,项目名称:niubility,代码行数:13,代码来源:robot.py
示例10: test_article
def test_article():
article = Article(
title="tt", description=to_binary("附近的萨卡里发生"), img="http", url="uuu"
)
assert article.render().strip() == to_text(
"""
<item>
<Title><![CDATA[tt]]></Title>
<Description><![CDATA[附近的萨卡里发生]]></Description>
<PicUrl><![CDATA[http]]></PicUrl>
<Url><![CDATA[uuu]]></Url>
</item>
"""
).strip()
开发者ID:whtsky,项目名称:WeRoBot,代码行数:14,代码来源:test_replies.py
示例11: test_message_crypt
def test_message_crypt():
encoding_aes_key = generate_token(32) + generate_token(11)
token = generate_token()
timestamp = to_text(int(time.time()))
nonce = generate_token(5)
app_id = generate_token(18)
crypt = MessageCrypt(
token=token, encoding_aes_key=encoding_aes_key, app_id=app_id
)
message = crypt.encrypt_message('hello', timestamp, nonce)
assert message is not None
message = parse_xml(message)
assert message is not None
message = crypt.decrypt_message(
message['TimeStamp'], message['Nonce'], message['MsgSignature'],
message['Encrypt']
)
assert message == to_binary('hello')
开发者ID:whtsky,项目名称:WeRoBot,代码行数:19,代码来源:test_crypto.py
示例12: encrypt_message
def encrypt_message(self, reply, timestamp=None, nonce=None):
"""
加密微信回复
:param reply: 加密前的回复
:type reply: WeChatReply 或 XML 文本
:return: 加密后的回复文本
"""
if hasattr(reply, "render"):
reply = reply.render()
timestamp = timestamp or to_binary(int(time.time()))
nonce = nonce or generate_token(5)
encrypt = to_text(self.prp_crypto.encrypt(reply, self.app_id))
signature = get_signature(self.token, timestamp, nonce, encrypt)
return to_text(self.ENCRYPTED_MESSAGE_XML.format(
encrypt=encrypt,
signature=signature,
timestamp=timestamp,
nonce=nonce
))
开发者ID:FlyRabbit,项目名称:WeRoBot,代码行数:20,代码来源:__init__.py
示例13: decrypt
def decrypt(self, text, app_id):
"""
对密文进行解密
:param text: 需要解密的密文
:param app_id: 微信公众平台的 AppID
:return: 解密后的字符串
"""
text = to_binary(text)
plain_text = self.cipher.decrypt(base64.b64decode(text))
padding = byte2int(plain_text, -1)
content = plain_text[16:-padding]
xml_len = socket.ntohl(struct.unpack("I", content[:4])[0])
xml_content = content[4:xml_len+4]
from_appid = content[xml_len+4:]
if to_text(from_appid) != app_id:
raise AppIdValidationError(text, app_id)
return xml_content
开发者ID:FlyRabbit,项目名称:WeRoBot,代码行数:21,代码来源:__init__.py
示例14: check_signature
def check_signature(self, timestamp, nonce, signature):
sign = [self.config["TOKEN"], timestamp, nonce]
sign.sort()
sign = to_binary(''.join(sign))
sign = hashlib.sha1(sign).hexdigest()
return sign == signature
开发者ID:railu,项目名称:WeRoBot,代码行数:6,代码来源:robot.py
示例15: unsubscribe
def unsubscribe(message):
logging.info('user %s unsubscribe.' % (message.source,))
id = to_binary(message.source)
session_storage = robot.config["SESSION_STORAGE"]
session_storage.delete(id)
return ''
开发者ID:zhu327,项目名称:ifwechat,代码行数:6,代码来源:wechat.py
示例16: remove_session
def remove_session(session):
try:
del session[to_binary("fromUser")]
except:
pass
开发者ID:bug3306,项目名称:WeRoBot,代码行数:5,代码来源:test_session.py
示例17: remove_session
def remove_session(session):
del session[to_binary("fromUser")]
开发者ID:CUCmehp,项目名称:WeRoBot,代码行数:2,代码来源:test_session.py
示例18: __init__
def __init__(self, filename='werobot_session'):
try:
self.db = dbm.open(filename, "c")
except TypeError: # pragma: no cover
# dbm in PyPy requires filename to be binary
self.db = dbm.open(to_binary(filename), "c")
开发者ID:bug3306,项目名称:WeRoBot,代码行数:6,代码来源:filestorage.py
注:本文中的werobot.utils.to_binary函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论