关闭防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
如果使用云服务器,可能还需要配置安全组
安装squid和apache
yum install -y squid
yum install -y httpd
创建用户
htpasswd -c /etc/squid/passwd squid_user
# 输入密码
# 文件 /etc/squid/passwd 会自动生成
配置squid授权
vi /etc/squid/squid.conf
修改以下几行
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd
acl auth_user proxy_auth REQUIRED
http_access allow auth_user
也可以直接把下面的内容复制到配置文件/etc/squid/squid.conf
中
acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7 # RFC 4193 local private network range
acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwd
acl auth_user proxy_auth REQUIRED
http_access allow auth_user
http_port 3128
coredump_dir /var/spool/squid
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|?) 0 0% 0
refresh_pattern . 0 20% 4320
启动squid
systemctl start squid
systemctl enable squid
# 查看状态
# systemctl status squid
在python爬虫中的使用
import requests
url = "https://baidu.com"
proxy_url = "http://[用户名]:[密码]@[服务器ip]:3128"
proxies = {"http": proxy_url, "https": proxy_url}
response = requests.get(url, proxies=proxies, headers=headers)
在Windows 10/11中的使用
网络 & Internet > 代理 > 手动设置代理,填写代理服务器IP和端口
在Mozilla Firefox中的使用
Mozilla Firefox可以使用系统代理,也可以单独设置
访问about:preferences#general
,点击最下方的网络设置,填写代理服务器IP和端口
发表评论