本文整理汇总了Python中twisted.python.util.InsensitiveDict.setdefault方法的典型用法代码示例。如果您正苦于以下问题:Python InsensitiveDict.setdefault方法的具体用法?Python InsensitiveDict.setdefault怎么用?Python InsensitiveDict.setdefault使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.python.util.InsensitiveDict
的用法示例。
在下文中一共展示了InsensitiveDict.setdefault方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RestClientFactory
# 需要导入模块: from twisted.python.util import InsensitiveDict [as 别名]
# 或者: from twisted.python.util.InsensitiveDict import setdefault [as 别名]
class RestClientFactory(protocol.ClientFactory):
protocol = client.HTTPPageGetter
noisy = False
def __init__(self, host, port, path, data, timeout = 60):
self.log = log.getChild('ClientFactory')
self.timeout = timeout
self.agent = "RestClient"
self.headers = InsensitiveDict()
self.headers.setdefault('Content-Length', len(data))
self.headers.setdefault("connection", "close")
self.method = 'POST'
self.url = 'http://' + host + ':' + str(port) + path
self.scheme = 'http'
self.postdata = data
self.host = host
self.port = port
self.path = path
self.waiting = 1
self.deferred = defer.Deferred()
self.response_headers = None
self.cookies = {}
def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, self.url)
def buildProtocol(self, addr):
p = protocol.ClientFactory.buildProtocol(self, addr)
if self.timeout:
timeoutCall = reactor.callLater(self.timeout, p.timeout)
self.deferred.addBoth(self._cancelTimeout, timeoutCall)
return p
def _cancelTimeout(self, result, timeoutCall):
if timeoutCall.active():
timeoutCall.cancel()
return result
def gotHeaders(self, headers):
self.response_headers = headers
def gotStatus(self, version, status):
self.version, self.status = version, status
def page(self, page):
if self.waiting:
self.waiting = 0
self.deferred.callback(page)
def noPage(self, reason):
if self.waiting:
self.waiting = 0
self.deferred.errback(reason)
def clientConnectionFailed(self, _, reason):
if self.waiting:
self.waiting = 0
self.deferred.errback(reason)
示例2: HeaderAwareHTTPClientFactory
# 需要导入模块: from twisted.python.util import InsensitiveDict [as 别名]
# 或者: from twisted.python.util.InsensitiveDict import setdefault [as 别名]
class HeaderAwareHTTPClientFactory(client.HTTPClientFactory):
protocol = yourHTTPPageGetter
noisy = False
def __init__(self, url, method='GET', postdata=None, headers=None,
agent="Coherence PageGetter", timeout=0, cookies=None,
followRedirect=True, redirectLimit=20):
self.followRedirect = followRedirect
self.redirectLimit = redirectLimit
self._redirectCount = 0
self.timeout = timeout
self.agent = agent
if cookies is None:
cookies = {}
self.cookies = cookies
if headers is not None:
self.headers = InsensitiveDict(headers)
else:
self.headers = InsensitiveDict()
if postdata is not None:
self.headers.setdefault('Content-Length', len(postdata))
# just in case a broken http/1.1 decides to keep connection alive
self.headers.setdefault("connection", "close")
self.postdata = postdata
self.method = method
self.setURL(url)
self.waiting = 1
self.deferred = defer.Deferred()
self._disconnectedDeferred = defer.Deferred()
self.response_headers = None
def buildProtocol(self, addr):
p = protocol.ClientFactory.buildProtocol(self, addr)
p.method = self.method
p.followRedirect = self.followRedirect
if self.timeout:
timeoutCall = reactor.callLater(self.timeout, p.timeout)
self.deferred.addBoth(self._cancelTimeout, timeoutCall)
self._disconnectedDeferred = defer.Deferred()
return p
def page(self, page):
if self.waiting:
self.waiting = 0
self.deferred.callback((page, self.response_headers))
示例3: test01_proxy
# 需要导入模块: from twisted.python.util import InsensitiveDict [as 别名]
# 或者: from twisted.python.util.InsensitiveDict import setdefault [as 别名]
def test01_proxy(self):
data = '{"type": "PacketPing"}'
headers = InsensitiveDict()
headers.setdefault('Content-Length', len(data))
headers.setdefault("connection", "close")
headers.setdefault("proxy-connection", "foo")
host = '127.0.0.1'
port = 19481
path = '/POKER_REST'
factory = pokerrestclient.PokerProxyClientFactory('POST', path, '1.1', headers, data, MockRequest(), 6, host + ':' + str(port) + path)
reactor.connectTCP(host, port, factory)
factory.deferred.addCallback(self.assertNotEquals, None)
return factory.deferred
示例4: HTTPClientFactory
# 需要导入模块: from twisted.python.util import InsensitiveDict [as 别名]
# 或者: from twisted.python.util.InsensitiveDict import setdefault [as 别名]
class HTTPClientFactory(protocol.ClientFactory):
"""Download a given URL.
@type deferred: Deferred
@ivar deferred: A Deferred that will fire when the content has
been retrieved. Once this is fired, the ivars `status', `version',
and `message' will be set.
@type status: str
@ivar status: The status of the response.
@type version: str
@ivar version: The version of the response.
@type message: str
@ivar message: The text message returned with the status.
@type response_headers: dict
@ivar response_headers: The headers that were specified in the
response from the server.
"""
protocol = HTTPPageGetter
url = None
scheme = None
host = ''
port = None
path = None
def __init__(self, url, method='GET', postdata=None, headers=None,
agent="Twisted PageGetter", timeout=0, cookies=None,
followRedirect=1, proxy=None):
self.protocol.followRedirect = followRedirect
self.timeout = timeout
self.agent = agent
self.proxy = proxy
if cookies is None:
cookies = {}
self.cookies = cookies
if headers is not None:
self.headers = InsensitiveDict(headers)
else:
self.headers = InsensitiveDict()
if postdata is not None:
self.headers.setdefault('Content-Length', len(postdata))
# just in case a broken http/1.1 decides to keep connection alive
self.headers.setdefault("connection", "close")
self.postdata = postdata
self.method = method
self.setURL(url)
self.waiting = 1
self.deferred = defer.Deferred()
self.response_headers = None
def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, self.url)
def setURL(self, url):
self.url = url
scheme, host, port, path = _parse(url)
if scheme and host:
self.scheme = scheme
self.host = host
self.port = port
if self.proxy:
self.path = "%s://%s:%s%s" % (self.scheme,
self.host,
self.port,
path)
else:
self.path = path
def buildProtocol(self, addr):
p = protocol.ClientFactory.buildProtocol(self, addr)
if self.timeout:
timeoutCall = reactor.callLater(self.timeout, p.timeout)
self.deferred.addBoth(self._cancelTimeout, timeoutCall)
return p
def _cancelTimeout(self, result, timeoutCall):
if timeoutCall.active():
timeoutCall.cancel()
return result
def gotHeaders(self, headers):
self.response_headers = headers
if headers.has_key('set-cookie'):
for cookie in headers['set-cookie']:
cookparts = cookie.split(';')
cook = cookparts[0]
cook.lstrip()
k, v = cook.split('=', 1)
self.cookies[k.lstrip()] = v.lstrip()
def gotStatus(self, version, status, message):
self.version, self.status, self.message = version, status, message
#.........这里部分代码省略.........
示例5: HTTPClientFactory
# 需要导入模块: from twisted.python.util import InsensitiveDict [as 别名]
# 或者: from twisted.python.util.InsensitiveDict import setdefault [as 别名]
class HTTPClientFactory(protocol.ClientFactory):
"""Download a given URL.
@type deferred: Deferred
@ivar deferred: A Deferred that will fire when the content has
been retrieved. Once this is fired, the ivars `status', `version',
and `message' will be set.
@type status: str
@ivar status: The status of the response.
@type version: str
@ivar version: The version of the response.
@type message: str
@ivar message: The text message returned with the status.
@type response_headers: dict
@ivar response_headers: The headers that were specified in the
response from the server.
@type method: str
@ivar method: The HTTP method to use in the request. This should be one of
OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, or CONNECT (case
matters). Other values may be specified if the server being contacted
supports them.
@type redirectLimit: int
@ivar redirectLimit: The maximum number of HTTP redirects that can occur
before it is assumed that the redirection is endless.
@type afterFoundGet: C{bool}
@ivar afterFoundGet: Deviate from the HTTP 1.1 RFC by handling redirects
the same way as most web browsers; if the request method is POST and a
302 status is encountered, the redirect is followed with a GET method
@type _redirectCount: int
@ivar _redirectCount: The current number of HTTP redirects encountered.
"""
protocol = HTTPPageGetter
url = None
scheme = None
host = ""
port = None
path = None
def __init__(
self,
url,
method="GET",
postdata=None,
headers=None,
agent="Twisted PageGetter",
timeout=0,
cookies=None,
followRedirect=True,
redirectLimit=20,
afterFoundGet=False,
):
self.followRedirect = followRedirect
self.redirectLimit = redirectLimit
self._redirectCount = 0
self.timeout = timeout
self.agent = agent
self.afterFoundGet = afterFoundGet
if cookies is None:
cookies = {}
self.cookies = cookies
if headers is not None:
self.headers = InsensitiveDict(headers)
else:
self.headers = InsensitiveDict()
if postdata is not None:
self.headers.setdefault("Content-Length", len(postdata))
# just in case a broken http/1.1 decides to keep connection alive
self.headers.setdefault("connection", "close")
self.postdata = postdata
self.method = method
self.setURL(url)
self.waiting = 1
self.deferred = defer.Deferred()
self.response_headers = None
def __repr__(self):
return "<%s: %s>" % (self.__class__.__name__, self.url)
def setURL(self, url):
self.url = url
scheme, host, port, path = _parse(url)
if scheme and host:
self.scheme = scheme
self.host = host
self.port = port
self.path = path
def buildProtocol(self, addr):
#.........这里部分代码省略.........
示例6: HTTPClientFactory
# 需要导入模块: from twisted.python.util import InsensitiveDict [as 别名]
# 或者: from twisted.python.util.InsensitiveDict import setdefault [as 别名]
class HTTPClientFactory(protocol.ClientFactory):
"""Download a given URL.
@type deferred: Deferred
@ivar deferred: A Deferred that will fire when the content has
been retrieved. Once this is fired, the ivars `status', `version',
and `message' will be set.
@type status: str
@ivar status: The status of the response.
@type version: str
@ivar version: The version of the response.
@type message: str
@ivar message: The text message returned with the status.
@type response_headers: dict
@ivar response_headers: The headers that were specified in the
response from the server.
@type method: str
@ivar method: The HTTP method to use in the request. This should be one of
OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, or CONNECT (case
matters). Other values may be specified if the server being contacted
supports them.
@type redirectLimit: int
@ivar redirectLimit: The maximum number of HTTP redirects that can occur
before it is assumed that the redirection is endless.
@type afterFoundGet: C{bool}
@ivar afterFoundGet: Deviate from the HTTP 1.1 RFC by handling redirects
the same way as most web browsers; if the request method is POST and a
302 status is encountered, the redirect is followed with a GET method
@type _redirectCount: int
@ivar _redirectCount: The current number of HTTP redirects encountered.
@ivar _disconnectedDeferred: A L{Deferred} which only fires after the last
connection associated with the request (redirects may cause multiple
connections to be required) has closed. The result Deferred will only
fire after this Deferred, so that callers can be assured that there are
no more event sources in the reactor once they get the result.
"""
protocol = HTTPPageGetter
url = None
scheme = None
host = ''
port = None
path = None
def __init__(self, url, method='GET', postdata=None, headers=None,
agent="Twisted PageGetter", timeout=0, cookies=None,
followRedirect=True, redirectLimit=20,
afterFoundGet=False):
self.followRedirect = followRedirect
self.redirectLimit = redirectLimit
self._redirectCount = 0
self.timeout = timeout
self.agent = agent
self.afterFoundGet = afterFoundGet
if cookies is None:
cookies = {}
self.cookies = cookies
if headers is not None:
self.headers = InsensitiveDict(headers)
else:
self.headers = InsensitiveDict()
if postdata is not None:
self.headers.setdefault('Content-Length', len(postdata))
# just in case a broken http/1.1 decides to keep connection alive
self.headers.setdefault("connection", "close")
self.postdata = postdata
self.method = method
self.setURL(url)
self.waiting = 1
self._disconnectedDeferred = defer.Deferred()
self.deferred = defer.Deferred()
# Make sure the first callback on the result Deferred pauses the
# callback chain until the request connection is closed.
self.deferred.addBoth(self._waitForDisconnect)
self.response_headers = None
def _waitForDisconnect(self, passthrough):
"""
Chain onto the _disconnectedDeferred, preserving C{passthrough}, so that
the result is only available after the associated connection has been
closed.
"""
self._disconnectedDeferred.addCallback(lambda ignored: passthrough)
return self._disconnectedDeferred
def __repr__(self):
#.........这里部分代码省略.........
示例7: IRCBot
# 需要导入模块: from twisted.python.util import InsensitiveDict [as 别名]
# 或者: from twisted.python.util.InsensitiveDict import setdefault [as 别名]
#.........这里部分代码省略.........
hops, gecos = hg.split(' ', 1)
user = self.get_user(nick) or IRCUser(self, nick)
user.username = username
user.hostname = host
user.oper = '*' in status
user.away = status[0] == 'G'
self.users[nick] = user
self.get_channel(channel)[nick] = IRCUserInChannel(user, channel)
self.parse_prefixes(user, nick, status[1:].replace('*', ''))
def modeChanged(self, user, channel, _set, modes, args):
args = list(args)
if channel not in self.parent.channel_map:
return
for m, arg in zip(modes, args):
if m in self.prefixes and arg != self.nickname:
u = self.get_user(arg).on(channel)
if u:
u.status = u.status.replace(self.prefixes[m], '')
if _set:
u.status = ''.join(sorted(list(u.status + self.prefixes[m]),
key=lambda k: self.priority[k]))
def has_status(self, nick, status):
if status != 0 and not status:
return True
if status not in self.priority:
return False
priority = self.priority[status]
u = self.users.get(nick, None)
return u and (u.priority is not None) and u.priority <= priority
def get_channel(self, channel):
return self.channels.setdefault(channel, InsensitiveDict())
def get_user(self, nick):
return self.users.get(nick, False)
def userJoined(self, user, channel):
nick = user.split('!')[0]
user = IRCUser(self, nick)
self.users[nick] = user
self.get_channel(channel)[nick] = IRCUserInChannel(user, channel)
def userRenamed(self, oldname, newname):
if oldname not in self.users:
return
u = self.users[oldname]
u.nick = newname
self.users[newname] = u
del self.users[oldname]
for k, v in self.channels.items():
if oldname in v:
v[newname] = v[oldname]
del v[oldname]
def userLeft(self, user, channel):
if user not in self.users:
return
del self.users[user]
for k, v in self.channels.items():
if user in v:
del v[user]
def userKicked(self, kickee, channel, kicker, message):
if kickee not in self.users: