美文网首页
代理池及squid的使用

代理池及squid的使用

作者: 都江堰古巨基 | 来源:发表于2018-08-22 18:20 被阅读0次

在爬虫的时候我们通常需要使用代理IP来避免被其他网站ban的情况,我们在其他网站上可以获取到免费的IP(成功率不高)或是使用收费的代理IP(如国内的芝麻代理,讯代理,蘑菇代理。。。)这些收费的代理IP通常使用成功率都比较高,而且比较稳定,但他们通常都是时间有限的,如3分钟,1分钟。。。
这样的话,我们需要随时都去维护一个可用的代理池,通常的思路是:
1.取其中一个IP,然后通过这个IP去访问一个网页,如果能访问,则是一个活跃的IP。
2.将这个IP存入,redis或者数据库。
3.爬虫使用的时候再去读这个数据库或者是redis。

若我们使用了squid这个代理服务器的话,将会变成这样:
1.获取一个IP,写入到squid的配置文件中(cache_peer)。
2.reload一下squid的配置。
3.爬虫这端只需要负责去使用squid提供的端口就行,squid会自动去除死掉的IP,使用活跃的IP。

举个栗子:

import requests

# 这里使用squid的端口是6666

r = requests.session()
r.get(url,proxy='127.0.0.1:6666')

# 这样的话发起请求的时候使用并不是本地实在的127.0.0.1:3268而是写入进去的一个代理ip。

centos下squid的一些常用的命令:
1.启动代理服务器:squid start
2.停止代理服务器:squid -k shutdown
3.重新启动代理服务器:service squid restart
4.后台运行代理服务器:squid -s
5.在前台启动代理服务器:squid -N -d1
6.重新加载代理服务器的配置文件:squid reload

squid的配置文件示例:

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
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access deny manager
# 不需要密码验证
http_access allow all

http_access allow localhost
# http_access deny all
http_port 6666
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 (Release|Packages(.gz)*)$      0       20%     2880
refresh_pattern .               0       20%     4320
#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
via off
forwarded_for off

request_header_access From deny all
request_header_access Server deny all
request_header_access WWW-Authenticate deny all
request_header_access Link deny all
request_header_access Cache-Control deny all
request_header_access Proxy-Connection deny all
request_header_access X-Cache deny all
request_header_access X-Cache-Lookup deny all
request_header_access Via deny all
request_header_access X-Forwarded-For deny all
request_header_access Pragma deny all
request_header_access Keep-Alive deny all

cache_mem 128 MB                            #内存中的缓存大小
maximum_object_size 16 MB                   #最大被缓存文件大小,这个配合下面的cache_dir使用,只作用于缓存到磁盘的文件。
cache_dir ufs /var/spool/squid 100 16 256   #缓存文件夹,默认是只在内存中进行缓存的。这里指定缓存大小为100M,第一层子目录为16个,第二层为256。   
access_log /var/log/squid/access.log        #定义访问日志路径
visible_hostname www.twy.com                #随意填写一个域名
cache_mgr 676534074@qq.com                  #随意写一个邮箱

# peers的配置
include /etc/squid/peers.conf

never_direct allow all

peers.conf文件如下:

cache_peer 60.169.248.135 parent 30591 0 no-query weighted-round-robin weight=1 connect-fail-limit=1 allow-miss max-conn=5

在squid中,reload的方式(切记ubanun和centos的不同!)

若是在ubanun的操作系统下(如docker中)reload的指令squid -k reconfigure
若是在centos中reload的指令service squid reload或者/bin/systemctl reload squid.service

相关文章

网友评论

      本文标题:代理池及squid的使用

      本文链接:https://www.haomeiwen.com/subject/apiyiftx.html