本文整理汇总了Python中pyxmpp.utils.to_utf8函数的典型用法代码示例。如果您正苦于以下问题:Python to_utf8函数的具体用法?Python to_utf8怎么用?Python to_utf8使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了to_utf8函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: complete_xml_element
def complete_xml_element(self, xmlnode, _unused):
"""Complete the XML node with `self` content.
Should be overriden in classes derived from `StanzaPayloadObject`.
:Parameters:
- `xmlnode`: XML node with the element being built. It has already
right name and namespace, but no attributes or content.
- `_unused`: document to which the element belongs.
:Types:
- `xmlnode`: `libxml2.xmlNode`
- `_unused`: `libxml2.xmlDoc`"""
if self.node:
xmlnode.setProp("node", to_utf8(self.node))
if self.sid:
xmlnode.setProp("sid", to_utf8(self.sid))
if self.mode and self.mode != 'tcp':
xmlnode.setProp("mode", to_utf8(self.mode))
for host in self.hosts:
try:
host.as_xml(xmlnode, _unused)
except:
pprint(host)
raise
if self.activate:
xmlnode.newChild(None, "activate", JID(self.activate).as_utf8())
if self.host_used:
h = xmlnode.newChild(None, "streamhost-used", None)
h.setProp("jid",JID(self.host_used).as_utf8())
示例2: complete_xml_element
def complete_xml_element(self, xmlnode, doc):
"""Complete the XML node with `self` content.
:Parameters:
- `xmlnode`: XML node with the element being built. It has already
right name and namespace, but no attributes or content.
- `doc`: document to which the element belongs.
:Types:
- `xmlnode`: `libxml2.xmlNode`
- `doc`: `libxml2.xmlDoc`"""
if self.type is not None and self.type not in self.allowed_types:
raise ValueError, "Invalid form field type: %r" % (self.type,)
if self.type is not None:
xmlnode.setProp("type", self.type)
if not self.label is None:
xmlnode.setProp("label", to_utf8(self.label))
if not self.name is None:
xmlnode.setProp("var", to_utf8(self.name))
if self.values:
if self.type and len(self.values) > 1 and not self.type.endswith(u"-multi"):
raise ValueError, "Multiple values not allowed for %r field" % (self.type,)
for value in self.values:
xmlnode.newTextChild(xmlnode.ns(), "value", to_utf8(value))
for option in self.options:
option.as_xml(xmlnode, doc)
if self.required:
xmlnode.newChild(xmlnode.ns(), "required", None)
if self.desc:
xmlnode.newTextChild(xmlnode.ns(), "desc", to_utf8(self.desc))
return xmlnode
示例3: __init__
def __init__(self, xmlnode = None, from_jid = None, to_jid = None, stanza_type = None,
stanza_id = None, show = None, status = None, priority = 0,
error = None, error_cond = None, stream = None):
"""Initialize a `Presence` object.
:Parameters:
- `xmlnode`: XML node to_jid be wrapped into the `Presence` object
or other Presence object to be copied. If not given then new
presence stanza is created using following parameters.
- `from_jid`: sender JID.
- `to_jid`: recipient JID.
- `stanza_type`: staza type: one of: None, "available", "unavailable",
"subscribe", "subscribed", "unsubscribe", "unsubscribed" or
"error". "available" is automaticaly changed to_jid None.
- `stanza_id`: stanza id -- value of stanza's "id" attribute
- `show`: "show" field of presence stanza. One of: None, "away",
"xa", "dnd", "chat".
- `status`: descriptive text for the presence stanza.
- `priority`: presence priority.
- `error_cond`: error condition name. Ignored if `stanza_type` is not "error"
:Types:
- `xmlnode`: `unicode` or `libxml2.xmlNode` or `Stanza`
- `from_jid`: `JID`
- `to_jid`: `JID`
- `stanza_type`: `unicode`
- `stanza_id`: `unicode`
- `show`: `unicode`
- `status`: `unicode`
- `priority`: `unicode`
- `error_cond`: `unicode`"""
self.xmlnode=None
if isinstance(xmlnode,Presence):
pass
elif isinstance(xmlnode,Stanza):
raise TypeError,"Couldn't make Presence from other Stanza"
elif isinstance(xmlnode,libxml2.xmlNode):
pass
elif xmlnode is not None:
raise TypeError,"Couldn't make Presence from %r" % (type(xmlnode),)
if stanza_type and stanza_type not in presence_types:
raise ValueError, "Invalid presence type: %r" % (type,)
if stanza_type=="available":
stanza_type=None
if xmlnode is None:
xmlnode="presence"
Stanza.__init__(self, xmlnode, from_jid = from_jid, to_jid = to_jid, stanza_type = stanza_type,
stanza_id = stanza_id, error = error, error_cond = error_cond, stream = stream)
if show:
self.xmlnode.newTextChild(common_ns,"show",to_utf8(show))
if status:
self.xmlnode.newTextChild(common_ns,"status",to_utf8(status))
if priority and priority!=0:
self.xmlnode.newTextChild(common_ns,"priority",to_utf8(unicode(priority)))
示例4: complete_xml_element
def complete_xml_element(self, xmlnode, _unused):
'to xml'
if isinstance(self.id, int) and self.id >=0:
xmlnode.setProp("id", to_utf8(str(self.id)))
else:
raise ValueError, "self.id must be int"
xmlnode.setProp("protocol", to_utf8(self.protocol)) if self.protocol else None
xmlnode.setProp("username", to_utf8(self.username)) if self.username else None
xmlnode.setProp("password", base64.b64encode(self.password)) if self.password else None
xmlnode.setProp("action", to_utf8(self.action)) if self.action else None
xmlnode.newTextChild(None, "data", binascii.b2a_base64(self.data)) if self.data is not None else None
示例5: __from_xml
def __from_xml(self, node):
if node.type!="element":
raise ValueError,"XML node is not an si (not en element)"
ns=get_node_ns_uri(node)
if ns and ns!=SI_NS or node.name!=self.xml_element_name:
raise ValueError,"XML node is not an si"
sid = node.prop("id")
self.sid = to_utf8(sid) if sid else None
mime_type = node.prop("mime-type")
self.mime_type = to_utf8(mime_type) if mime_type else None
profile_ns = node.prop("profile")
self.profile_ns = to_utf8(profile_ns) if profile_ns else None
示例6: _plain_auth_stage2
def _plain_auth_stage2(self, _unused):
"""Do the second stage (<iq type='set'/>) of legacy "plain"
authentication.
[client only]"""
iq=Iq(stanza_type="set")
q=iq.new_query("jabber:iq:auth")
q.newTextChild(None,"username",to_utf8(self.my_jid.node))
q.newTextChild(None,"resource",to_utf8(self.my_jid.resource))
q.newTextChild(None,"password",to_utf8(self.password))
self.send(iq)
self.set_response_handlers(iq,self.auth_finish,self.auth_error)
iq.free()
示例7: _auth_stage1
def _auth_stage1(self):
"""Do the first stage (<iq type='get'/>) of legacy ("plain" or
"digest") authentication.
[client only]"""
iq=Iq(stanza_type="get")
q=iq.new_query("jabber:iq:auth")
q.newTextChild(None,"username",to_utf8(self.my_jid.node))
q.newTextChild(None,"resource",to_utf8(self.my_jid.resource))
self.send(iq)
self.set_response_handlers(iq,self.auth_stage2,self.auth_error,
self.auth_timeout,timeout=60)
iq.free()
示例8: __init__
def __init__(self, xmlnode = None, from_jid = None, to_jid = None, stanza_type = None, stanza_id = None,
subject = None, body = None, thread = None, error = None, error_cond = None, stream = None):
"""Initialize a `Message` object.
:Parameters:
- `xmlnode`: XML node to_jid be wrapped into the `Message` object
or other Message object to be copied. If not given then new
presence stanza is created using following parameters.
- `from_jid`: sender JID.
- `to_jid`: recipient JID.
- `stanza_type`: staza type: one of: "get", "set", "result" or "error".
- `stanza_id`: stanza id -- value of stanza's "id" attribute. If not
given, then unique for the session value is generated.
- `subject`: message subject,
- `body`: message body.
- `thread`: message thread id.
- `error_cond`: error condition name. Ignored if `stanza_type` is not "error".
:Types:
- `xmlnode`: `unicode` or `libxml2.xmlNode` or `Stanza`
- `from_jid`: `JID`
- `to_jid`: `JID`
- `stanza_type`: `unicode`
- `stanza_id`: `unicode`
- `subject`: `unicode`
- `body`: `unicode`
- `thread`: `unicode`
- `error_cond`: `unicode`"""
self.xmlnode=None
if isinstance(xmlnode,Message):
pass
elif isinstance(xmlnode,Stanza):
raise TypeError, "Couldn't make Message from other Stanza"
elif isinstance(xmlnode,libxml2.xmlNode):
pass
elif xmlnode is not None:
raise TypeError, "Couldn't make Message from %r" % (type(xmlnode),)
if xmlnode is None:
xmlnode="message"
Stanza.__init__(self, xmlnode, from_jid = from_jid, to_jid = to_jid, stanza_type = stanza_type,
stanza_id = stanza_id, error = error, error_cond = error_cond, stream = stream)
if subject is not None:
self.xmlnode.newTextChild(common_ns,"subject",to_utf8(subject))
if body is not None:
self.xmlnode.newTextChild(common_ns,"body",to_utf8(body))
if thread is not None:
self.xmlnode.newTextChild(common_ns,"thread",to_utf8(thread))
示例9: _digest_auth_stage2
def _digest_auth_stage2(self, _unused):
"""Do the second stage (<iq type='set'/>) of legacy "digest"
authentication.
[client only]"""
iq=Iq(stanza_type="set")
q=iq.new_query("jabber:iq:auth")
q.newTextChild(None,"username",to_utf8(self.my_jid.node))
q.newTextChild(None,"resource",to_utf8(self.my_jid.resource))
digest = hashlib.sha1(to_utf8(self.stream_id)+to_utf8(self.password)).hexdigest()
q.newTextChild(None,"digest",digest)
self.send(iq)
self.set_response_handlers(iq,self.auth_finish,self.auth_error)
iq.free()
示例10: xpath_eval
def xpath_eval(self,expr,namespaces=None):
"""Evaluate XPath expression on the error element.
The expression will be evaluated in context where the common namespace
(the one used for stanza elements, mapped to 'jabber:client',
'jabber:server', etc.) is bound to prefix "ns" and other namespaces are
bound accordingly to the `namespaces` list.
:Parameters:
- `expr`: the XPath expression.
- `namespaces`: prefix to namespace mapping.
:Types:
- `expr`: `unicode`
- `namespaces`: `dict`
:return: the result of the expression evaluation.
"""
ctxt = common_doc.xpathNewContext()
ctxt.setContextNode(self.xmlnode)
ctxt.xpathRegisterNs("ns",to_utf8(self.ns))
if namespaces:
for prefix,uri in namespaces.items():
ctxt.xpathRegisterNs(prefix,uri)
ret=ctxt.xpathEval(expr)
ctxt.xpathFreeContext()
return ret
示例11: __init__
def __init__(self,xmlnode_or_cond,error_type=None,copy=1,parent=None):
"""Initialize a StreamErrorNode object.
:Parameters:
- `xmlnode_or_cond`: XML node to be wrapped into this object
or the primary (defined by XMPP specification) error condition name.
- `error_type`: type of the error, one of: 'cancel', 'continue',
'modify', 'auth', 'wait'.
- `copy`: When `True` then the XML node will be copied,
otherwise it is only borrowed.
- `parent`: Parent node for the XML node to be copied or created.
:Types:
- `xmlnode_or_cond`: `libxml2.xmlNode` or `unicode`
- `error_type`: `unicode`
- `copy`: `bool`
- `parent`: `libxml2.xmlNode`"""
if type(xmlnode_or_cond) is str:
xmlnode_or_cond=unicode(xmlnode_or_cond,"utf-8")
if type(xmlnode_or_cond) is unicode:
if not stanza_errors.has_key(xmlnode_or_cond):
raise ValueError, "Bad error condition"
ErrorNode.__init__(self,xmlnode_or_cond,STANZA_ERROR_NS,copy=copy,parent=parent)
if type(xmlnode_or_cond) is unicode:
if error_type is None:
error_type=stanza_errors[xmlnode_or_cond][1]
self.xmlnode.setProp("type",to_utf8(error_type))
示例12: do_bind
def do_bind(self,stanza):
"""Do the resource binding requested by a client connected.
[server only]
:Parameters:
- `stanza`: resource binding request stanza.
:Types:
- `stanza`: `pyxmpp.Iq`"""
fr=stanza.get_from()
if fr and fr!=self.peer:
r=stanza.make_error_response("forbidden")
self.send(r)
r.free()
return
resource_n=stanza.xpath_eval("bind:bind/bind:resource",{"bind":BIND_NS})
if resource_n:
resource=resource_n[0].getContent()
else:
resource="auto"
if not resource:
r=stanza.make_error_response("bad-request")
else:
self.unset_iq_set_handler("bind",BIND_NS)
r=stanza.make_result_response()
self.peer.set_resource(resource)
q=r.new_query(BIND_NS,"bind")
q.newTextChild(None,"jid",to_utf8(self.peer.as_unicode()))
self.state_change("authorized",self.peer)
r.set_to(None)
self.send(r)
r.free()
示例13: _parse_response
def _parse_response(self,response):
"""Parse a client reponse and pass to further processing.
:Parameters:
- `response`: the response from the client.
:Types:
- `response`: `str`
:return: a challenge, a success indicator or a failure indicator.
:returntype: `sasl.Challenge`, `sasl.Success` or `sasl.Failure`"""
response=response.split('\x00')[0] # workaround for some SASL implementations
if self.realm:
realm=to_utf8(self.realm)
realm=_quote(realm)
else:
realm=None
username=None
cnonce=None
digest_uri=None
response_val=None
authzid=None
nonce_count=None
while response:
m=_param_re.match(response)
if not m:
self.__logger.debug("Response syntax error: %r" % (response,))
return Failure("not-authorized")
response=m.group("rest")
var=m.group("var")
val=m.group("val")
self.__logger.debug("%r: %r" % (var,val))
if var=="realm":
realm=val[1:-1]
elif var=="cnonce":
if cnonce:
self.__logger.debug("Duplicate cnonce")
return Failure("not-authorized")
cnonce=val[1:-1]
elif var=="qop":
if val!='auth':
self.__logger.debug("qop other then 'auth'")
return Failure("not-authorized")
elif var=="digest-uri":
digest_uri=val[1:-1]
elif var=="authzid":
authzid=val[1:-1]
elif var=="username":
username=val[1:-1]
elif var=="response":
response_val=val
elif var=="nc":
nonce_count=val
self.last_nonce_count+=1
if int(nonce_count)!=self.last_nonce_count:
self.__logger.debug("bad nonce: %r != %r"
% (nonce_count,self.last_nonce_count))
return Failure("not-authorized")
return self._check_params(username,realm,cnonce,digest_uri,
response_val,authzid,nonce_count)
示例14: send_si_request
def send_si_request(self, session) :
'''
<iq type='set' id='offer1' to='[email protected]/resource'>
<si xmlns='http://jabber.org/protocol/si' id='a0' mime-type='text/plain' profile='http://jabber.org/protocol/si/profile/file-transfer'>
<file xmlns='http://jabber.org/protocol/si/profile/file-transfer' name='test.txt' size='1022' hash='552da749930852c69ae5d2141d3766b1' date='1969-07-21T02:56:15Z'>
<desc>This is a test. If this were a real file...</desc>
</file>
<feature xmlns='http://jabber.org/protocol/feature-neg'>
<x xmlns='jabber:x:data' type='form'>
<field var='stream-method' type='list-single'>
<option><value>>>http://jabber.org/protocol/bytestreams</value></option>
<option><value>>>http://jabber.org/protocol/ibb</value></option>
</field>
</x>
</feature>
</si>
</iq>
'''
stream = self.client.get_stream()
#new_id = stream.generate_id()
iq = Iq(None, session.jid, session.to_jid, "set", session.sid)
si_node = iq.add_new_content(SI_NS, 'si')
si_node.setProp("id", session.sid)
si_node.setProp("mime-type", 'application/octet-stream')
si_node.setProp("profile", FILE_TRANSFER_NS)
file_node = si_node.newChild(None,"file",None)
file_node.setProp("xmlns", FILE_TRANSFER_NS)
file_node.setProp("name", to_utf8(session.file_name))
file_node.setProp("size", to_utf8(session.file_size))
feature_node = si_node.newChild(None, "feature", None)
feature_node.setProp("xmlns", FEATURE_NS)
form = Form()
form.add_field( name = 'stream-method', field_type = 'list-single',
#options = [Option('http://jabber.org/protocol/bytestreams', None), Option('http://jabber.org/protocol/ibb', None)])
options = [Option('http://jabber.org/protocol/ibb', None)])
form.as_xml(feature_node)
stream.set_response_handlers(iq, self.received_si_success, self.received_si_error)
stream.send(iq)
self.sessions[session.sid] = session
示例15: complete_xml_element
def complete_xml_element(self, xmlnode, doc):
xmlnode.setProp("type", self.type) if self.type else None
xmlnode.setProp("value", self.value) if self.value else None
xmlnode.setProp("action", self.action)
xmlnode.setProp("order", to_utf8(self.order))
[xmlnode.newChild(None, child, None)
for child in PRIVACY_TYPES
if getattr(self, util.pythonize(child))]