Nginx常用模塊的使用(五):對IP地址和瀏覽器的限制
- 對瀏覽器進行限制
- 對IP地址的限制
- 對爬蟲進行限制
- python爬蟲偽裝成其他瀏覽器
對瀏覽器進行限制
組態檔的修改
server {
listen 80;
server_name www.pp.com alias web.pp.com;
location / {
root html/pp.com;
index index.html index.htm;
}
#對瀏覽器進行限制,現在只允許谷歌瀏覽器
if ($http_user_agent !~* Chrome) {
return 404;
}
}
對IP地址的限制
組態檔的修改
```bash
server {
listen 80;
server_name www.pp.com alias web.pp.com;
location / {
root html/pp.com;
index index.html index.htm;
}
#對ip地址進行限制
if ($remote_addr ~* 192.168.0.1[0-9]) {
return 404;
}
}
對爬蟲進行限制
組態檔的修改
server {
listen 80;
server_name www.pp.com alias web.pp.com;
location / {
root html/pp.com;
index index.html index.htm;
}
if ($http_user_agent ~* python-requests) {
return 404;
}
}
python爬蟲偽裝成其他瀏覽器
>>> import requests
>>> headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36"}
>>> result = requests.get("http://www.pp.com", headers=headers)
>>> result.text
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/293557.html
標籤:其他
上一篇:socket實作TCP/IP通信
