Skip to content
This repository has been archived by the owner on Dec 22, 2017. It is now read-only.

Two ways to use this proxy in Python #3

Closed
scturtle opened this issue May 10, 2016 · 8 comments
Closed

Two ways to use this proxy in Python #3

scturtle opened this issue May 10, 2016 · 8 comments

Comments

@scturtle
Copy link

After got the secret sauce:

basic_auth, proxy, port = get_proxy()

I find that one low-level way to use it is:

req = 'GET {url} HTTP/1.0\nProxy-Authorization: BASIC {basic_auth}\n\n'.format(
    url='http://httpbin.org/ip', basic_auth=basic_auth)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock = ssl.wrap_socket(sock)
sock.connect((proxy, port))
sock.send(req.encode('ascii'))
print(sock.recv(1024))
sock.close()

Another method needs an ugly monkey patch of HTTPSConnection to avoid SNI error:

class PatchedContext:

    def __init__(self, conn):
        conn._check_hostname = False
        conn._context.check_hostname = False
        self.ctx = conn._context

    def __getattr__(self, attr):
        if attr == 'wrap_socket':
            return lambda sock, server_hostname: self.ctx.wrap_socket(sock)
        return getattr(self.ctx, attr)

conn = http.client.HTTPSConnection(proxy, port)
conn._context = PatchedContext(conn)
conn.request("GET", "http://httpbin.org/ip",
             headers={"Proxy-Authorization": "BASIC "+basic_auth})
print(conn.getresponse().read())
@dessant
Copy link

dessant commented May 16, 2016

@scturtle, were you able to use it with https sites? https://httpbin.org/ip and others result in 500 Internal Server Error.

@scturtle
Copy link
Author

@dessant I can use it as plain HTTP/HTTPS proxy now:) See my gist.

@dessant
Copy link

dessant commented May 17, 2016

@scturtle oh lovely, thanks for sharing. 🏆

@spaze
Copy link
Owner

spaze commented May 17, 2016

@scturtle Thanks for sharing! Any idea how can I package it with my script together? Or do an update to docs or something?

@scturtle
Copy link
Author

@spaze Not really. Just share here in case someone needs. Pls feel free to take any part of the code or close this issue.

@ucupumar
Copy link

Thanks for sharing! If possible, can you do another example but using python requests?

@0at
Copy link

0at commented Jan 16, 2017

I tried the above code changes and still seem to be getting a b'malformed HTTP response ""\n'. Is there something that I'm missing?

I used the proxy.py script in the gist you made and it seems to work when I do that. So not sure why there would be something specific to doing the monkey patch (I've tested to make sure that the proxy credentials are valid)

@kapkirl
Copy link

kapkirl commented Aug 28, 2017

With http resources everything is fine, but with https - malformed HTTP response "<html>"
Any ideas how to handle that?

@spaze spaze closed this as completed Dec 22, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants