Skip to content

Instantly share code, notes, and snippets.

@ksato9700
Created December 13, 2011 05:18
Show Gist options
  • Save ksato9700/1470727 to your computer and use it in GitHub Desktop.
Save ksato9700/1470727 to your computer and use it in GitHub Desktop.
pyuv example
import pyuv
def on_close(tcp):
print "closed"
def on_read(tcp, data):
print "read"
if data is None:
tcp.close(on_close)
else:
print data
def on_write(tcp, status):
print "written"
tcp.start_read(on_read)
def on_connection(tcp, status):
print "connected"
req_message = "GET / HTTP/1.0\r\n\r\n"
tcp.write(req_message, on_write)
def main():
server_addr = ("127.0.0.1", 80)
loop = pyuv.Loop.default_loop()
tcp = pyuv.TCP(loop)
tcp.connect(server_addr, on_connection)
print "main"
loop.run()
if __name__ == "__main__":
main()
import pyuv
import sys
def on_close(tcp):
print "closed"
def main():
server_addr = ("127.0.0.1", 80)
loop = pyuv.Loop.default_loop()
tcp = pyuv.TCP(loop)
tcp.connect(server_addr, lambda tcp, status:
(sys.stdout.write("connected\n"),
tcp.write("GET / HTTP/1.0\r\n\r\n", lambda tcp, status:
(sys.stdout.write("written\n"),
tcp.start_read(lambda tcp, data:
(sys.stdout.write("read\n"),
tcp.close(lambda tcp: sys.stdout.write("closed\n"))
if data is None else sys.stdout.write(data + "\n")))))))
print "main"
loop.run()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment