Skip to content

Instantly share code, notes, and snippets.

@Lukasa Lukasa/test.py
Created Aug 15, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.