一个小型HTTP代理实现
项目描述
本模块通过扩展BaseHTTPServer实现了一个小型HTTP代理。支持GET、HEAD、POST、PUT、DELETE和CONNECT方法。
最新版本可以通过PyPI安装
$ pip install httproxy
或者
$ easy_install httproxy
源代码仓库和问题跟踪器维护在GitHub上。
快速入门
用法
httproxy [options] httproxy [options] <allowed-client> ...
选项
-h, --help                   Show this screen.
--version                    Show version and exit.
-H, --host HOST              Host to bind to [default: 127.0.0.1].
-p, --port PORT              Port to bind to [default: 8000].
-l, --logfile PATH           Path to the logfile [default: STDOUT].
-i, --pidfile PIDFILE        Path to the pidfile [default: httproxy.pid].
-d, --daemon                 Daemonize (run in the background). The
                             default logfile path is httproxy.log in
                             this case.
-c, --configfile CONFIGFILE  Path to a configuration file.
-v, --verbose                Log headers.
要启动代理服务器并将其绑定到端口22222(它将监听和接受连接的端口)
httproxy -p 22222
要启动代理服务器,将其绑定到端口22222并告诉它将所有请求记录到文件httproxy.log
httproxy -p 22222 -l httproxy.log
启动代理服务器,仅允许来自IP 123.123.123.123 的连接
httproxy 123.123.123.123
启动绑定到端口22222的代理服务器,日志记录到文件 httproxy.log,并在后台(作为守护进程)运行服务器
httproxy -p 22222 -l httproxy.log -d
配置文件
所有作为命令行参数声明的选项都可以通过配置文件传递。httproxy将查找以下文件以读取配置
- /etc/httproxy/config 
- $HOME/.httproxy/config(或在Windows上为%HOME%\.httproxy\config) 
- 在命令行上的--configfile中指定的值 
主部分中设置的名称来源于长命令行选项名称。
allowed-clients部分包含可以访问代理的主机名列表,每行一个主机名。删除此部分或留空以允许任何客户端连接。
示例文件
[main] host = localhost port = 8011 logfile = /Users/ambv/.httproxy/log pidfile = /Users/ambv/.httproxy/pid daemon = yes verbose = yes [allowed-clients] localhost 192.168.0.1
注意:命令行选项优先于配置文件设置。
可选依赖
如果您安装了setproctitle,则ps报告的进程名称将更加详细。
如果您安装了psutil,httproxy将能够在启动时自动删除旧的pid文件。
变更日志
0.9.0
- 从文件中读取配置的能力(--configfile) 
- 指定代理将绑定到的地址的能力(--host) 
- 记录发送和接收的标题的能力(--verbose) 
- 更好的进程管理:pid文件支持,更详细的进程标题(带有可选的setproctitle依赖项) 
- 修复了虚假的[Errno 54] Connection reset by peer回溯 
- 在接收到SIGHUP、SIGINT或SIGTERM时正确关闭 
- 主要代码重构 
- 仅与Python 2.6和2.7兼容:需要docopt和configparser 
0.3.1
- 添加了基本的FTP文件检索 
- 添加了自定义日志方法 
- 添加了使其作为独立应用程序运行的代码 
由Mitko Haralanov在2009年升级。
0.2.1
- 基本版本由原作者在2006年托管在http://www.oki-osk.jp/esc/python/proxy/