My favorites | Sign in
Project Home Downloads Wiki Issues Source
New issue   Search
for
  Advanced search   Search tips   Subscriptions

Issue 1598 attachment: test_http_agent.py (2.5 KB)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from twisted.python.log import err
from twisted.web.client import Agent
from twisted.internet import reactor, defer
from twisted.internet.ssl import ClientContextFactory
from StringIO import StringIO
from twisted.web.client import FileBodyProducer
from twisted.web.http_headers import Headers
import sys

class WebClientContextFactory(ClientContextFactory):
def getContext(self, hostname, port):
return ClientContextFactory.getContext(self)

def display(response):
print "Received response"
print response

def setupheader(host):
return Headers({'Connection': ['keep-alive'],
'Content-Length': ['80'],
'Accept-Encoding': ['gzip,text'],
'Connection': ['keep-alive'],
'Accept': ['application/json','application/jsonrequest'],
'User-Agent': ['Twisted/txwebservice'],
'Host': [host],
'Content-Type': ['application/json; charset=utf-8']})

body='{"params": [{"id": 50670}], "jsonrpc": "2.0", "method": "changeDetail", "id": 1}'

def d(agent,url,bodyProducer):
bp = bodyProducer(body)
d = agent.request("POST", url,
setupheader(url[len("https://"):]),
bp)
d.addCallbacks(display, err)
d.addCallback(lambda ignored: reactor.stop())
return d

class strBodyProducer():
def __init__(self, s):
self.s = s
self.length = len(s)

def startProducing(self,encoder):
def callback(_):
encoder.write(self.s)
return None
d = defer.Deferred()
d.addCallback(callback)
reactor.callLater(1.0,
d.callback,
None)
return d
def pauseProducing(self):
pass

def resumeProducing(self):
pass

def stopProducing(self):
pass

def main():
argvl=len(sys.argv)
def usage():
raise Exception("Usage %s <url> ?-d")
if argvl not in [2,3]:
usage()
srvurl = sys.argv[1]
if not srvurl.endswith("/"):
srvurl+="/"
url=srvurl+"gerrit/rpc/ChangeDetailService"
contextFactory = WebClientContextFactory()
agent = Agent(reactor, contextFactory)
if argvl == 2:
_ = d(agent,url,lambda s:FileBodyProducer(StringIO(body)))
elif sys.argv[2] == "-d":
_ = d(agent,url,lambda s:strBodyProducer(body))
else:
usage()
reactor.run()

if __name__ == "__main__":
main()
Powered by Google Project Hosting