Last active
October 30, 2019 11:53
-
-
Save fabiocerqueira/df60a48c7ab3ead62406849f3c0ae794 to your computer and use it in GitHub Desktop.
twisted example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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