牧羊岛

懒是不会有好果子吃滴//

导航

win7、linux安装使用pip、mitmproxy

Posted on 2015-01-21 17:17  牧羊岛  阅读(21020)  评论(0编辑  收藏  举报

安装pip https://pip.pypa.io/en/latest/installing.html

步骤:

下载 https://bootstrap.pypa.io/get-pip.py

python get-pip.py
D:\pip>python get-pip.py
Collecting pip
  Downloading pip-6.0.6-py2.py3-none-any.whl (1.3MB)
    100% |################################| 1.3MB 74kB/s ta 0:00:011
Collecting setuptools
  Downloading setuptools-12.0.4-py2.py3-none-any.whl (502kB)
    100% |################################| 503kB 117kB/s ta 0:00:01
Installing collected packages: setuptools, pip


Successfully installed pip-6.0.6 setuptools-12.0.4
python -m pip
Usage:
  D:\Python27\python.exe -m pip <command> [options]

Commands:
  install                     Install packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  zip                         DEPRECATED. Zip individual packages.
  unzip                       DEPRECATED. Unzip individual packages.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring
                              environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be
                              used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output.
  --log <path>                Path to a verbose appending log.
  --proxy <proxy>             Specify a proxy in the form
                              [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should
                              attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists:
                              (s)witch, (i)gnore, (w)ipe, (b)ackup.
  --trusted-host <hostname>   Mark this host as trusted, even though it does
                              not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file
                              containing the private key and the certificate
                              in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine
                              whether a new version of pip is available for
                              download. Implied with --no-index.

安装依赖包

python -m pip install netlib pyopenssl pyasn1 urwid pil lxml flask

错误:

File "D:\Python27\lib\mimetypes.py", line 249, in enum_types
ctype = ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 1: ordinal not in range(128)

解决方案:

打开D:\Python27\lib\mimetypes.py文件,在256行,将

default_encoding = sys.getdefaultencoding()

改为

        if sys.getdefaultencoding() != 'gbk':  
            reload(sys)  
            sys.setdefaultencoding('gbk')  
        default_encoding = sys.getdefaultencoding()

 由于pil安装出错,所以先:

python -m pip install netlib pyopenssl pyasn1 urwid lxml flask

然后:

python -m pip install pyamf protobuf

又出错:

error: Microsoft Visual C++ 9.0 is required (Unable to find vcvarsall.bat).
Get it from http://aka.ms/vcpython27

需要装 Microsoft Visual C++ Compiler for Python 2.7(共 83.8 MB)

http://www.microsoft.com/en-us/download/confirmation.aspx?id=44266

再试:

python -m pip install pyamf protobuf

下一步:

python -m pip install pil --allow-external PIL --allow-unverified PIL

下一步:

python -m pip install nose pathod countershape

最后:

python -m pip install mitmproxy

 

参考资料:

https://pip.pypa.io/en/latest/installing.html
http://m.blog.csdn.net/blog/roler_/40398789
http://netsecurity.51cto.com/art/201311/416548.htm
http://www.ajucs.com/archives/565.html

——————————————————————————————————

Linux:

类似windows就是pip命令不用加前面的python -m ,如果不是管理员需要sudo。

在pip install netlib pyopenssl pyasn1 urwid lxml flask的时候遇到的错误:

src/lxml/lxml.etree.c:8:22: 错误:pyconfig.h:没有那个文件或目录

yum install python-devel

c/_cffi_backend.c:13:17: 错误:ffi.h:没有那个文件或目录

yum install libffi-devel

其他的还有:

yum install gcc
yum install libxml2
yum install libxslt-devel libxml2-devel

然后:

pip install pil --allow-external PIL --allow-unverified PIL
pip install pyamf protobuf
pip install nose pathod countershape
pip install mitmproxy

“pip install mitmproxy”遇到错误:

[root@centos64 ~]# pip install mitmproxy
Collecting mitmproxy
Using cached mitmproxy-0.11.3.tar.gz
Traceback (most recent call last):
File "<string>", line 20, in <module
Complete output from command python setup.py egg_info:
Traceback (most recent call last):

File "<string>", line 20, in <module

----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-QBFasU/mitmproxy

查网上说mitmproxy需要python2.7而CentOS6.4默认是python2.6,所以一下部分是CentOS6.4安装python2.7与python2.6共存的办法:

安装必备的开发工具,据说都需要,以防万一,我都安装了:

yum groupinstall "Development tools"
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel

下载和安装Python,注意命令“make altinstall”据说是防止/usr/bin/下面有多个版本的python

cd ~
wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar zxvf Python-2.7.9.tgz 
cd Python-2.7.9
./configure --prefix=/usr/local
make && make altinstall

如果需要更改系统python版本

mv /usr/bin/python /usr/bin/python2.6.6.old
ln -s /usr/local/bin/python27 /usr/bin/python
vi /usr/bin/yum

#!/usr/bin/python改为#!/usr/bin/python2.6,因为yum需要python2.6

安装pip

wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py
python2.7 ez_setup.py
easy_install-2.7 pip

注意后面使用pip2.7而不是pip。

提示

-bash: /usr/bin/pip2.7: 没有那个文件或目录

的话退出重新登录就好了。安装mitmproxy

pip2.7 install netlib pyopenssl pyasn1 urwid lxml flask
pip2.7 install pil --allow-external PIL --allow-unverified PIL
pip2.7 install pyamf protobuf
pip2.7 install nose pathod countershape
pip2.7 install mitmproxy

 

参考资料:

http://web.49jie.com/?p=1644
http://segmentfault.com/blog/digwtx/1190000000654227
http://stackoverflow.com/questions/27144574/mitmproxy-installation-error

————————————————————————————

使用:

顺便装下图形界面

 

yum groupinstall "Desktop"
yum groupinstall "X Window System"
yum groupinstall "Chinese Support"
startx

 

http代理

mitmproxy

快捷键

q - 返回上一层
enter - 查看内容
Tab - 切换看request/response
pageup - 上翻页
pagedown - 下翻页
C - 清空(注意大写)

透明代理

http://mitmproxy.org/doc/transparent/linux.html

sysctl -w net.ipv4.ip_forward=1
echo 0 | sudo tee /proc/sys/net/ipv4/conf/*/send_redirects
service iptables start
service iptables save
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8080
service iptables save
cat /etc/sysconfig/iptables
mitmproxy -T --host

大概的意思就是开启转发,设置路由规则,开启mitmproxy的透明代理模式。

iptables如果是直接编辑文件:

-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT 

 

-A PREROUTING -i eth0 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080
-A PREROUTING -i eth0 -p tcp -m tcp --dport 443 -j REDIRECT --to-ports 8080

 使用中还有这么个错误

File "/usr/local/lib/python2.7/threading.py", line 810, in __bootstrap_inner
File "/usr/local/lib/python2.7/site-packages/libmproxy/controller.py", line 77, in run
File "/usr/local/lib/python2.7/site-packages/netlib/tcp.py", line 477, in serve_forever
File "/usr/local/lib/python2.7/socket.py", line 202, in accept
error: [Errno 24] 打开的文件过多

ulimit -n 65535

不过是临时修改

 

安装证书 

mitmproxy-ca.pem PEM格式的私钥和证书。
mitmproxy-CA-cert.pem PEM格式的证书。使用此分发大多数非Windows平台。
mitmproxy-CA-cert.p12在PKCS12格式的证书。在Windows上使用。
mitmproxy-CA-cert.cer相同的文件质子交换膜,但预计一些Android设备的扩展。