Skip to content

Instantly share code, notes, and snippets.

@Lukasa
Created August 15, 2016 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lukasa/eee9a020c69388c6c9f0e2cdeac9103c to your computer and use it in GitHub Desktop.
Save Lukasa/eee9a020c69388c6c9f0e2cdeac9103c to your computer and use it in GitHub Desktop.
Blue or Red?
from twisted.internet.protocol import Protocol
class PrinterProtocol(Protocol):
def connectionMade(self, transport):
self.transport = transport
self.response = b''
self.transport.write(
b'GET / HTTP/1.1\r\n'
b'Host: http2bin.org\r\n'
b'Connection: close\r\n'
b'\r\n'
)
def dataReceived(self, data):
self.response += data
def connectionLost(self, reason):
pass
import socket
def request_data():
p = PrinterProtocol()
s = socket.create_connection(('http2bin.org', 80))
writer = s.makefile('w+b', bufsize=0)
p.connectionMade(writer)
while True:
data = s.recv(8192)
if data:
p.dataReceived(data)
else:
p.connectionLost(None)
break
s.close()
return p.response
def main():
print "one"
print request_data()
print "two"
print request_data()
print "three"
print request_data()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment