Created
August 15, 2016 10:41
-
-
Save Lukasa/eee9a020c69388c6c9f0e2cdeac9103c to your computer and use it in GitHub Desktop.
Blue or Red?
This file contains 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.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