代理

来源:互联网 发布:c语言流程图怎么画 编辑:程序博客网 时间:2024/05/19 13:25

1. pip 安装python模块使用代理

pip install <包名> --proxy http://localhost:1050/

你使用shadowsocks代理时,默认cmd不会使用代理(不管是在pac模式还是全局模式下);为了使用你的ss代理pip请求远程url,输入上面命令。1050是我的ss使用的本地端口。

2. git使用代理

git config --global http.proxy 'socks5://127.0.0.1:1080' #设置代理
git config --global --unset http.proxy #取消代理

3. Python代码里使用代理

首先安装PySocks和requests包

$ pip install 'requests[socks]'

然后代码如下:

# -*- coding: utf-8 -*-import requestsurl = 'http://www.google.com/'http_proxy = 'socks5://127.0.0.1:1080'  # 使用socks代理proxy = {    "http": http_proxy,    "https": http_proxy,}text = requests.get(url, proxies=proxy).textprint(text)

我本地ss端口是1080,这样,就可以在python代码里访问google了。

4. Ubuntu下终端设置代理

ubuntu下设置好ss,你不能直接在设置http_proxy=socks5://127.0.0.1:1080代理终端,好像windows下可以(自己测试)。反正linux下,你还需要安装polipo。

  1. 安装polipo
    sudo apt-get install polipo
  2. 编辑polipo配置文件
    sudo vim /etc/polipo/config
    在config文件最开始添加下面两行

    socksParentProxy = "localhost:1080"socksProxyType = socks5
  3. 启动polipo
    service polipo start
    (关闭服务是service polipo stop)
  4. 设置代理
    export http_proxy=http://localhost:8123
    export https_proxy=http://localhost:8123
    这里8123是polipo监听默认端口号
  5. 验证是否成功
    curl www.google.com.hk
    如果返回goolge首页源码,则证明代理成功。

5. Mac OSX下设置终端代理

和上面linux同样的道理,osx终端也不能直接使用ss的代理。我们依然需要polipo。

  1. 安装polipo
    sudo brew install polipo
  2. 设置polipo使用ss端口
    polipo socksParentProxy=127.0.0.1:1080
    维持此终端,打开另一个端口进行测试
  3. 重新打开一个终端,设置http_proxy
    export http_proxy="http://127.0.0.1:8123/"
    export https_proxy="http://127.0.0.1:8123/"
  4. 验证是否成功
    curl www.google.com.hk
    如果返回goolge首页源码,则证明代理成功。

更详细见 http://www.jianshu.com/p/9918f6e25c45



Ref

http://liuxiaohui.net/2016/07/27/Make-the-python-requests-work-via-socks-proxy-on-CentOS-server.html
http://droidyue.com/blog/2016/04/04/set-shadowsocks-proxy-for-terminal/index.html
http://www.jianshu.com/p/9918f6e25c45

0 0