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 HTTPBasicAuth

url = '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 password


def 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 password
    password = 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>
"""
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值