views:

287

answers:

3

Hey guys,

I am little stumped: I have a simple messenger client program (pure python, sockets), and I wanted to add proxy support (http/s, socks), however I am a little confused on how to go about it. I am assuming that the connection on the socket level will be done to the proxy server, at which point the headers should contain a CONNECT + destination IP (of the chat server) and authentication, (if proxy requires so), however the rest is a little beyond me. How is the subsequent connection handled, specifically the reading/writing, etc...

Are there any guides on proxy support implementation for socket based (tcp) programming in Python?

Thank you

+2  A: 

Maybe use something like SocksiPy which does all the protocol details for you and would let you connect through a SOCKS proxy as you would without it?

Vin-G
This works well. I was able to integrate everything, and the code of SocksiPy shows exactly what happens during the tunneling of HTTP/SOCKS. I am very surprised I have not found it before, thanks!
Terry Felkrow
A: 

Have a look at stunnel.

Stunnel can allow you to secure non-SSL aware daemons and protocols (like POP, IMAP, LDAP, etc) by having Stunnel provide the encryption, requiring no changes to the daemon's code

Anurag Uniyal
+1  A: 

It is pretty simple - after you send the HTTP request: CONNECT example.com:1234 HTTP/1.0\r\nHost: example.com:1234\r\n<additional headers incl. authentication>\r\n\r\n, the server responds with HTTP/1.0 200 Connection established\r\n\r\n and then (after the double line ends) you can communicate just as you would communicate with example.com port 1234 without the proxy (as I understand you already have the client-server communication part done).

Messa
Thank you for this answer, much appreciated. I had to accept the previous poster though, because I was able to integrate his solution quite easily and the code even demonstrates SOCKS4/5 handling, in addition to the HTTP tunneling.
Terry Felkrow