Skip to content

Instantly share code, notes, and snippets.

@fabiocerqueira
Last active October 30, 2019 11:53
Show Gist options
  • Save fabiocerqueira/df60a48c7ab3ead62406849f3c0ae794 to your computer and use it in GitHub Desktop.
Save fabiocerqueira/df60a48c7ab3ead62406849f3c0ae794 to your computer and use it in GitHub Desktop.
twisted example
from twisted.internet import reactor, defer
from twisted.web.client import Agent
def ping_google():
agent = Agent(reactor)
d = agent.request("GET", "https://www.google.com/")
def check(response):
print("finish request")
return response.code == 200
d.addCallback(check)
return d
@defer.inlineCallbacks
def main():
is_ok = yield ping_google()
if is_ok:
print("Google is ok")
else:
print("Google is down")
reactor.stop()
if __name__ == "__main__":
reactor.callLater(0, main)
reactor.run()
from twisted.internet import reactor, defer
from twisted.web.client import Agent
@defer.inlineCallbacks
def ping_google():
agent = Agent(reactor)
response = yield agent.request("GET", "https://www.google.com/")
print("finish request")
defer.returnValue(response.code == 200)
@defer.inlineCallbacks
def main():
is_ok = yield ping_google()
if is_ok:
print("Google is ok")
else:
print("Google is down")
reactor.stop()
if __name__ == "__main__":
reactor.callLater(0, main)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment