代理和换源

前言

此文章旨在记录一些常用的设置代理的方法,方便随时查阅。

pip

临时换源

你可以在运行pip命令时,使用-i参数指定源地址。例如:

1
pip install <package_name> -i <mirror_url>

例如,从国内的阿里云镜像安装requests包,可以这样操作:

1
pip install requests -i https://mirrors.aliyun.com/pypi/simple

你还可以通过设置环境变量来临时指定pip源。这种方法的好处是你可以在一次会话中多次使用该源,而无需每次都指定。
在Linux或macOS系统上,可以这样操作:

1
2
export PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple
pip install requests

在Windows系统上,可以这样操作:

1
2
set PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple 
pip install requests

这种方法的好处是你可以在一次会话中多次使用该源,而无需每次都指定。

永久换源

你可以在pip的配置文件中设置默认的源地址。pip的配置文件通常位于以下路径:

Linux和macOS: ~/.pip/pip.conf
Windows: %APPDATA%\pip\pip.ini

如果这些文件不存在,可以手动创建。在文件中添加以下内容:

1
[global] index-url = https://mirrors.aliyun.com/pypi/simple 

你还可以通过pip命令来设置默认源:

1
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple 

执行上述命令后,pip会自动创建或更新配置文件,并将默认源设置为你指定的地址。

常见的国内镜像源

以下是一些常用的国内PyPI镜像源,你可以根据需要选择使用:

  • 阿里云: https://mirrors.aliyun.com/pypi/simple/
  • 豆瓣: https://pypi.douban.com/simple/
  • 清华大学: https://pypi.tuna.tsinghua.edu.cn/simple/
  • 中国科学技术大学: https://pypi.mirrors.ustc.edu.cn/simple/

还原默认源

查看当前的源地址:

1
pip config get global.index-url

恢复默认源:

1
pip config unset global.index-url

npm

在发表npm包时,注意把源改回官方源才能发布,因此不推荐直接修改源,而是使用代理或者其他包管理器。

你可以直接修改npm的源地址。在命令行中输入以下命令:

1
npm config set registry https://registry.npmmirror.com

在命令行中输入以下命令设置代理

1
2
npm config set proxy http://127.0.0.1:1080
npm config set https-proxy https://127.0.0.1:1080

或者你也可以在环境变量中设置代理

1
2
3
4
5
6
# linux/macos
export http_proxy=http://127.0.0.1:1080
export https_proxy=https://127.0.0.1:1080
# windows
set http_proxy=http://127.0.0.1:1080
set https_proxy=https://127.0.0.1:1080

查看代理:

1
2
npm config get proxy
npm config get https-proxy

删除代理:

1
2
npm config delete proxy
npm config delete https-proxy

安装cnpm包来代替npm,然后使用cnpm来安装依赖。

1
2
npm install -g cnpm --registry=https://registry.npmmirror.com
cnpm install <package_name>

nrm是npm registry manager的简称,它可以帮助你管理npm源。

1
2
npm install -g nrm --registry=https://registry.npmmirror.com
nrm use taobao

测试速度:

1
nrm test taobao

使用nrm添加镜像地址:

1
2
nrm add r_name r_url 
# r_name 为镜像名字,r_url 为镜像地址

删除nrm镜像:

1
nrm del r_name

还原默认源

查看当前的源地址:

1
npm config get registry

恢复默认源:

1
npm config set registry https://registry.npmjs.org

git

设置代理

1
2
git config --global http.proxy http://127.0.0.1:1080
git config --global https.proxy https://127.0.0.1:1080

取消代理

1
2
3
4
# 取消HTTP代理设置
git config --global --unset http.proxy
# 取消HTTPS代理设置
git config --global --unset https.proxy

Python

requests模块的代理设置如下

1
2
3
4
5
6
7
8
9
import requests

proxies = {
"http": "http://127.0.0.1:1080",
"https": "https://127.0.0.1:1080",
}

response = requests.get("https://www.google.com", proxies=proxies)
print(response.text)

httpx的使用方法与requests类似,只需将requests替换为httpx即可,代理格式可以简化,而且它还支持异步请求。

1
2
3
4
5
6
7
8
9
10
11
12
import httpx

proxies = "http://127.0.0.1:1080"

# 同步
response = httpx.get("https://www.google.com", proxies=proxies)
print(response.text)

# 异步
async with httpx.AsyncClient(proxies=proxies) as client:
response = await client.get("https://www.google.com")
print(response.text)

aiohttp是Python异步HTTP客户端/服务器框架,它可以帮助你更方便地处理HTTP请求。

1
2
3
4
5
6
7
import aiohttp

proxies = "http://127.0.0.1:1080"

async with aiohttp.ClientSession() as session:
async with session.get("https://www.google.com", proxy=proxies) as response:
print(await response.text())

环境变量代理

Linux和Windows如果直接在系统环境设置代理可能会出现问题,本文仅介绍临时代理的设置

1
2
export http_proxy=http://127.0.0.1:1080
export https_proxy=https://127.0.0.1:1080

取消代理:

1
2
unset http_proxy
unset https_proxy
1
2
set http_proxy=http://127.0.0.1:1080
set https_proxy=https://127.0.0.1:1080

取消代理:

1
2
set http_proxy=
set https_proxy=