本文整理汇总了Python中six.b方法的典型用法代码示例。如果您正苦于以下问题:Python six.b方法的具体用法?Python six.b怎么用?Python six.b使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six
的用法示例。
在下文中一共展示了six.b方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: squash_commands
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def squash_commands(commands):
output = []
buf = EMPTY
for c in commands:
buf = EMPTY.join((buf, SYM_STAR, b(str(len(c))), SYM_CRLF))
for arg in map(encode, c):
if len(buf) > 6000 or len(arg) > 6000:
output.append(
EMPTY.join((buf, SYM_DOLLAR, b(str(len(arg))), SYM_CRLF)))
output.append(arg)
buf = SYM_CRLF
else:
buf = EMPTY.join((buf, SYM_DOLLAR, b(str(len(arg))), SYM_CRLF,
arg, SYM_CRLF))
output.append(buf)
return output
示例2: test_kv_encoding
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def test_kv_encoding(self, consul_port):
c = consul.Consul(port=consul_port)
# test binary
c.kv.put('foo', struct.pack('i', 1000))
index, data = c.kv.get('foo')
assert struct.unpack('i', data['Value']) == (1000,)
# test unicode
c.kv.put('foo', u'bar')
index, data = c.kv.get('foo')
assert data['Value'] == six.b('bar')
# test empty-string comes back as `None`
c.kv.put('foo', '')
index, data = c.kv.get('foo')
assert data['Value'] is None
# test None
c.kv.put('foo', None)
index, data = c.kv.get('foo')
assert data['Value'] is None
# check unencoded values raises assert * Python3 don't need
# pytest.raises(AssertionError, c.kv.put, 'foo', {1: 2})
示例3: test_kv_recurse
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def test_kv_recurse(self, consul_port):
c = consul.Consul(port=consul_port)
index, data = c.kv.get('foo/', recurse=True)
assert data is None
c.kv.put('foo/', None)
index, data = c.kv.get('foo/', recurse=True)
assert len(data) == 1
c.kv.put('foo/bar1', '1')
c.kv.put('foo/bar2', '2')
c.kv.put('foo/bar3', '3')
index, data = c.kv.get('foo/', recurse=True)
assert [x['Key'] for x in data] == [
'foo/', 'foo/bar1', 'foo/bar2', 'foo/bar3']
assert [x['Value'] for x in data] == [
None, six.b('1'), six.b('2'), six.b('3')]
示例4: test_acl_translate
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def test_acl_translate(self, acl_consul):
c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
payload = """
agent "" {
policy = "write"
}
"""
translate = c.acl.create_translate(
payload=payload, token=acl_consul.token)
assert translate == b'agent_prefix "" {\n policy = "write"\n}'
# fixme
pytest.raises(consul.ConsulException,
c.acl.get_translate,
c.acl.self()['AccessorID'],
acl_consul.token)
示例5: test_kv_recurse
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def test_kv_recurse(self, acl_consul):
c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
index, data = c.kv.get('foo/', recurse=True)
assert data is None
c.kv.put('foo/', None)
index, data = c.kv.get('foo/', recurse=True)
assert len(data) == 1
c.kv.put('foo/bar1', '1')
c.kv.put('foo/bar2', '2')
c.kv.put('foo/bar3', '3')
index, data = c.kv.get('foo/', recurse=True)
assert [x['Key'] for x in data] == [
'foo/', 'foo/bar1', 'foo/bar2', 'foo/bar3']
assert [x['Value'] for x in data] == [
None, six.b('1'), six.b('2'), six.b('3')]
c.kv.delete('foo')
c.kv.delete('foo/')
c.kv.delete('foo/bar1')
c.kv.delete('foo/bar2')
c.kv.delete('foo/bar3')
示例6: test_session_delete_ttl_renew
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def test_session_delete_ttl_renew(self, acl_consul):
c = consul.Consul(port=acl_consul.port, token=acl_consul.token)
s = c.session.create(behavior='delete', ttl=20)
# attempt to renew an unknown session
pytest.raises(consul.NotFound, c.session.renew, '1' * 36)
session = c.session.renew(s)
assert session['Behavior'] == 'delete'
assert session['TTL'] == '20s'
# trying out the behavior
assert c.kv.put('foo', '1', acquire=s) is True
index, data = c.kv.get('foo')
assert data['Value'] == six.b('1')
c.session.destroy(s)
index, data = c.kv.get('foo')
assert data is None
示例7: test_kv_missing
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def test_kv_missing(self, loop, consul_port):
c = consul.tornado.Consul(port=consul_port)
@gen.coroutine
def main():
yield c.kv.put('index', 'bump')
index, data = yield c.kv.get('foo')
assert data is None
index, data = yield c.kv.get('foo', index=index)
assert data['Value'] == six.b('bar')
loop.stop()
@gen.coroutine
def put():
yield c.kv.put('foo', 'bar')
loop.add_timeout(time.time() + (2.0 / 100), put)
loop.run_sync(main)
示例8: test_kv_subscribe
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def test_kv_subscribe(self, loop, consul_port):
c = consul.tornado.Consul(port=consul_port)
@gen.coroutine
def get():
index, data = yield c.kv.get('foo')
assert data is None
index, data = yield c.kv.get('foo', index=index)
assert data['Value'] == six.b('bar')
loop.stop()
@gen.coroutine
def put():
response = yield c.kv.put('foo', 'bar')
assert response is True
loop.add_timeout(time.time() + (1.0 / 100), put)
loop.run_sync(get)
示例9: test_kv_subscribe
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def test_kv_subscribe(self, loop, consul_port):
async def get():
c = consul.aio.Consul(port=consul_port, loop=loop)
fut = asyncio.ensure_future(put(), loop=loop)
index, data = await c.kv.get('foo')
assert data is None
index, data = await c.kv.get('foo', index=index)
assert data['Value'] == six.b('bar')
await fut
async def put():
c = consul.aio.Consul(port=consul_port, loop=loop)
await asyncio.sleep(1.0 / 100, loop=loop)
response = await c.kv.put('foo', 'bar')
assert response is True
loop.run_until_complete(get())
示例10: request
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def request(self, callback, method, url, **kwargs):
if 'data' in kwargs and not isinstance(kwargs['data'], bytes):
# python2/3 compatibility
data = kwargs.pop('data')
kwargs['data'] = data.encode(encoding='utf-8') \
if hasattr(data, 'encode') else b(data)
try:
response = yield self.client.request(method, url, **kwargs)
parsed = yield self._get_resp(response)
returnValue(callback(self.response(*parsed)))
except ConnectError as e:
raise ConsulException(
'{}: {}'.format(e.__class__.__name__, e.message))
except ResponseNeverReceived:
# this exception is raised if the connection to the server is lost
# when yielding a response, this could be due to network issues or
# server restarts
raise ConsulException(
'Server connection lost: {} {}'.format(method.upper(), url))
except RequestTransmissionFailed:
# this exception is expected if the reactor is stopped mid request
raise ConsulException(
'Request incomplete: {} {}'.format(method.upper(), url))
示例11: _test_instance_get_operation
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def _test_instance_get_operation(self, operation, getter, key, utcnow):
self.set_mock_db_items(fakes.DB_INSTANCE_2)
os_instance_2 = fakes.OSInstance(fakes.OS_INSTANCE_2)
self.nova.servers.get.return_value = os_instance_2
getter.return_value = 'fake_data'
utcnow.return_value = datetime.datetime(2015, 1, 19, 23, 34, 45, 123)
resp = self.execute(operation,
{'InstanceId': fakes.ID_EC2_INSTANCE_2})
expected_data = (base64.b64encode(six.b(getter.return_value))
.decode("utf-8"))
self.assertEqual({'instanceId': fakes.ID_EC2_INSTANCE_2,
'timestamp': '2015-01-19T23:34:45.000Z',
key: expected_data},
resp)
self.db_api.get_item_by_id.assert_called_once_with(
mock.ANY, fakes.ID_EC2_INSTANCE_2)
self.nova.servers.get.assert_called_once_with(fakes.ID_OS_INSTANCE_2)
getter.assert_called_once_with(os_instance_2)
示例12: test_authentication_start_pack
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def test_authentication_start_pack():
username = 'user123'
packed = TACACSAuthenticationStart(
username,
TAC_PLUS_AUTHEN_TYPE_ASCII
).packed
assert packed == six.b(
'\x01' # tacacs_plus.flags.TAC_PLUS_AUTHEN_LOGIN \
'\x00' # tacacs_plus.flags.TAC_PLUS_PRIV_LVL_MIN \
'\x01' # tacacs_plus.flags.TAC_PLUS_AUTHEN_TYPE_ASCII \
'\x01' # tacacs_plus.flags.TAC_PLUS_AUTHEN_SVC_LOGIN \
'\x07' # username_len == 7 \
'\x0b' # port_len == 11 \
'\x0d' # rem_addr_len == 13 \
'\x00' # data_len == 0 \
'user123' # username \
'python_tty0' # port \
'python_device' # rem_addr
)
示例13: test_authenticate_pap
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def test_authenticate_pap(fake_socket, packets):
"""
client -> AUTHSTART (user+pass)
STATUS_PASS <- server
"""
client = TACACSClient('127.0.0.1', 49, None, session_id=12345)
client._sock = fake_socket
reply = client.authenticate('username', 'pass',
authen_type=TAC_PLUS_AUTHEN_TYPE_PAP)
assert reply.valid
fake_socket.buff.seek(0)
first_header = TACACSHeader.unpacked(fake_socket.buff.read(12))
assert (first_header.version_max, first_header.version_min) == (12, 1)
first_body = fake_socket.buff.read(first_header.length)
assert TACACSAuthenticationStart(
'username',
TAC_PLUS_AUTHEN_TYPE_PAP,
data=six.b('pass')
).packed == first_body
示例14: test_authorize_chap
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def test_authorize_chap(fake_socket, packets):
client = TACACSClient('127.0.0.1', 49, None, session_id=12345)
client._sock = fake_socket
reply = client.authorize('username', arguments=[b"service=shell", b"cmd=show", b"cmdargs=version"],
authen_type=TAC_PLUS_AUTHEN_TYPE_CHAP)
assert reply.valid
fake_socket.buff.seek(0)
first_header = TACACSHeader.unpacked(fake_socket.buff.read(12))
assert (first_header.version_max, first_header.version_min) == (12, 0)
first_body = fake_socket.buff.read(first_header.length)
assert TACACSAuthorizationStart(
'username', TAC_PLUS_AUTHEN_METH_TACACSPLUS, TAC_PLUS_PRIV_LVL_MIN,
TAC_PLUS_AUTHEN_TYPE_CHAP, [b"service=shell", b"cmd=show", b"cmdargs=version"],
).packed == first_body
# test client.account
示例15: packed
# 需要导入模块: import six [as 别名]
# 或者: from six import b [as 别名]
def packed(self):
# 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
#
# +----------------+----------------+----------------+----------------+
# | user_msg len | data len |
# +----------------+----------------+----------------+----------------+
# | flags | user_msg ...
# +----------------+----------------+----------------+----------------+
# | data ...
# +----------------+
# B = unsigned char
# !H = network-order (big-endian) unsigned short
# s = char[]
password = six.b(self.password)
data = self.data
return (
struct.pack('!H', len(password)) +
struct.pack('!H', len(data)) +
struct.pack('B', self.flags) +
struct.pack('%ds' % len(password), password) +
struct.pack('%ds' % len(data), data)
)