Python,为基于套接字的应用程序实现代理支持(不是 urllib2)
我有点困惑:我有一个简单的信使客户端程序(纯Python,套接字),我想添加代理支持(http/s,socks),但是我对如何去做有点困惑。我假设套接字级别的连接将完成到代理服务器,此时标头应包含 CONNECT + 目标 IP(聊天服务器的)和身份验证(如果代理需要如此),但是其余的就是有点超出我的范围了。如何处理后续连接,特别是读/写等...
是否有关于 Python 中基于套接字(tcp)编程的代理支持实现的指南?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许使用类似 SocksiPy 的东西,它会为您完成所有协议详细信息,并让您通过 SOCKS 代理进行连接没有它你会吗?
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?
这非常简单 - 发送 HTTP 请求后: CONNECT example.com:1234 HTTP/1.0\r\nHost: example.com:1234\r\n\r\n\r\n,服务器响应
HTTP/1.0 200 Connectionbuilt\r\n\r\n
然后(双行结束后)您可以像在没有代理的情况下与 example.com 端口 1234 进行通信一样进行通信(据我所知,您已经完成了客户端-服务器通信部分)。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 withHTTP/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).看看 stunnel。
Have a look at stunnel.