Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A crude proxy protocol v1 test client
#!/usr/bin/env python
import socket
import ssl
import sys
from urlparse import urlparse
'''Crude test client to a HTTP(S) server listening for proxy protocol
connections. Takes a HTTP(S) url as an argument and prints out the raw
$ https://localhost/ | head -1
HTTP/1.1 200 OK
class ProxyHTTPRequest:
def __init__(self, url):
parsed = urlparse(url)
dst = parsed.netloc
if ':' in parsed.netloc:
dst, dstPort = dst.split(':')
dstPort = {'https': 443, 'http': 80}[parsed.scheme]
self.parsed = parsed
self.srcPort = 33333
self.src = ''
self.dst = socket.gethostbyname(dst)
self.dstPort = dstPort
def proxy_request(self):
'Generates the proxy protocol string'
data = map(str, ('PROXY', 'TCP4', self.src, self.dst, self.srcPort,
return ' '.join(data) + '\r\n'
def connect(self):
'''Creates the TCP connection, sends the proxy protocol string then
upgrades the socket to SSL if appropriate'''
self.conn = socket.create_connection((self.dst, self.dstPort))
if self.parsed.scheme == 'https':
self.conn = ssl.wrap_socket(self.conn)
def get(self):
'Sends a simple HTTP request and prints out the raw response'
self.conn.send('GET {0} HTTP/1.0\r\n\r\n'.format(self.parsed.path))
out = ''
while True:
lastRead = self.conn.recv()
if not lastRead:
out += lastRead
return out
if __name__ == '__main__':
print ProxyHTTPRequest(sys.argv[1]).get()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment