python--杂识--16--代理密码中包含特殊字符
1 安装nginx
2 centos环境安装
yum install httpd-tools
3 nginx.conf
/etc/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {upstream backend {server 127.0.0.1:80;}server {listen 80;server_name localhost;location / {return 200 'Hello, World!';}}server {listen 514;server_name localhost;auth_basic "Restricted Access"; # 要求输入用户名和密码auth_basic_user_file /etc/nginx/.htpasswd; # 存储用户名和密码的文件路径location / {proxy_pass http://backend;}}}
4 代理设置密码
测出设置的 test/123abc@
htpasswd -c /etc/nginx/.htpasswd test
New password:
Re-type new password:
Adding password for user test
5 启动nginx[代理]
检查配置文件是否正确
/usr/local/nginx/sbin/nginx -t -c /etc/nginx/conf/nginx.conf
启动nginx
/usr/local/nginx/sbin/nginx -c /etc/nginx/conf/nginx.conf
6 curl测试
(base) [root@Chasing-Dreams conf]# curl -X GET http://test:123abc%40@localhost:514
Hello, World!
7 python代码测试
import requests
from requests.auth import HTTPBasicAuthurl = 'http://localhost'
username = "test"
password = "123abc@"def special_char(password):tmp_password_list = list()for x in password:if not x.isalnum():x = "%" + hex(ord(x))[2:]tmp_password_list.append(x)password = "".join(tmp_password_list)return passworddef method1():print("===========1=============")proxy = {'http': 'http://localhost:514','https': 'https://localhost:514'}print(proxy)response = requests.get(url, proxies=proxy, auth=HTTPBasicAuth(username, password))print(response.text)def method2():print("===========2=============")print("===========2.1=============")url = "http://%s:%s@localhost:514" % (username, password) # 代理print(url)response = requests.get(url=url)print(response.text)print("===========2.2=============")password_ = special_char(password)url = "http://%s:%s@localhost:514" % (username, password_)print(url)response = requests.get(url=url)print(response.text)def method3():print("===========3=============")proxy = {'http': 'http://localhost:514','https': 'https://localhost:514'}print(proxy)req = requests.request(method="GET", url=url, proxies=proxy, auth=HTTPBasicAuth(username, password))print(req.text)def method4():print("===========4=============")"""部分代理按该方式处理,使用nginx做代理测试时,没有成功"""global passwordpassword = special_char(password)proxy = {'http': 'http://%s:%s@localhost:514' % (username, password),'https': 'https://%s:%s@localhost:514' % (username, password)}print(proxy)req = requests.request(method="GET", url=url, proxies=proxy)print(req.text)if __name__ == "__main__":method1()method2()method3()method4()"""
运行结果:
(base) [root@Chasing-Dreams test]# python test.py
===========1=============
{'http': 'http://localhost:514', 'https': 'https://localhost:514'}
Hello, World!
===========2=============
===========2.1=============
http://test:123abc@@localhost:514
Hello, World!
===========2.2=============
http://test:123abc%40@localhost:514
Hello, World!
===========3=============
{'http': 'http://localhost:514', 'https': 'https://localhost:514'}
Hello, World!
===========4=============
{'http': 'http://test:123abc%40@localhost:514', 'https': 'https://test:123abc%40@localhost:514'}
<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.20.2</center>
</body>
</html>
"""