32

squid正向代理访问外网

 3 years ago
source link: http://www.cnblogs.com/lemon-le/p/13419003.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

目录

一、前言

二、Squid安装

三、客户端设置

四、验证

五、Python请求

六、写在最后

一、前言

Eb2yAbJ.png!web

1)我们可能会碰到这样的需求,客户端Client-1需要访问Server-1,但由于各种各样的原因,不能直接通信。于是想到在中间找一台服务器做正向代理,客户端Clent1通过代理服务器与服务端Server1通信。

2)我们选择做正向代理的方式有很多,但我们这里选择Squid,可以通过性能测试等对比多个方案选出适合自己的。

二、Squid的安装

1、安装

方法一、

yum install -y gcc openssl openssl-devel
yum install squid

方法二、

wget http://www.squid-cache.org/Versions/v4/squid-4.12.tar.gz
tar -zxvf squid-4.12.tar.gz 
cd squid-4.12/
./configure --enable-ssl --with-openssl=/usr/local/openssl
make
make install

注: /usr/local/openssl指的是你的openssl目录,有的是/usr/include/openssl;如果指定不正确的话,编译会一直报错

2、修改配置文件

vim /usr/local/squid/etc/squid.conf  添加下面这一行允许所有访问,配置文件的访问策略,权限控制,缓存等后续博文更新,这里比较松的允许所有请求访问。

http_access allow all

3、启动squid

./squid

三、客户端设置

我使用google浏览器设置squid代理,设置-->高级-->打开您的计算机代理设置;squid的默认端口为3128;

f6VRBrr.png!web

四、验证

通过IP即可知道是不是自己设置的代理服务器。

BJ3MnqR.png!web

五、Python使用代理访问

import requests

# 使用代理获取百度首页
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
}
#  定义代理的字典
proxies = {
    "https": "http://xxxxx:3128"
}
# 使用代理给服务器发送请求
response = requests.get("https://www.baidu.com", proxies=proxies, headers=headers, timeout=(3, 7))
# 获取状态
print(response.status_code)
print(response.content.decode())

六、写在最后

在平凡中坚持前行,总有一天会遇见不一样的自己。

写博客记录、思考、总结,趟过的坑不趟第二遍。

所有的文章,皆同步在公众号“运维汪”,可关注;也可加入“不扯淡,专注于技术”的QQ群:753512236

Ur6Bjeu.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK