| Server IP : 20.75.53.88 / Your IP : 216.73.217.72 [ Web Server : Apache System : Linux VMRALP-3 4.4.0-256-generic #290~14.04.1-Ubuntu SMP Thu Jun 20 09:24:50 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 5.5.9-1ubuntu4.29+esm15 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority, Domains : 3 Domains MySQL : ON | cURL : OFF | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /usr/share/doc/python-twisted-web/examples/ |
Upload File : |
#!/usr/bin/env python
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
This example demonstrates how to make a simple http client.
Usage:
httpclient.py <url>
Don't forget the http:// when you type the web address!
"""
import sys
from pprint import pprint
from twisted import version
from twisted.python import log
from twisted.internet.defer import Deferred
from twisted.internet import reactor
from twisted.internet.protocol import Protocol
from twisted.web.iweb import UNKNOWN_LENGTH
from twisted.web.http_headers import Headers
from twisted.web.client import Agent, ResponseDone
class WriteToStdout(Protocol):
def connectionMade(self):
self.onConnLost = Deferred()
def dataReceived(self, data):
"""
Print out the html page received.
"""
print 'Got some:', data
def connectionLost(self, reason):
if not reason.check(ResponseDone):
reason.printTraceback()
else:
print 'Response done'
self.onConnLost.callback(None)
def main(reactor, url):
"""
We create a custom UserAgent and send a GET request to a web server.
"""
userAgent = 'Twisted/%s (httpclient.py)' % (version.short(),)
agent = Agent(reactor)
d = agent.request(
'GET', url, Headers({'user-agent': [userAgent]}))
def cbResponse(response):
"""
Prints out the response returned by the web server.
"""
pprint(vars(response))
proto = WriteToStdout()
if response.length is not UNKNOWN_LENGTH:
print 'The response body will consist of', response.length, 'bytes.'
else:
print 'The response body length is unknown.'
response.deliverBody(proto)
return proto.onConnLost
d.addCallback(cbResponse)
d.addErrback(log.err)
d.addBoth(lambda ign: reactor.callWhenRunning(reactor.stop))
reactor.run()
if __name__ == '__main__':
main(reactor, *sys.argv[1:])