分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

urllib库认证,代理,cookie

发布时间:2023-09-06 02:29责任编辑:白小东关键词:url

认证,代理,cookie

 1from urllib.request import HTTPBasicAuthHandler, HTTPPasswordMgrWithDefaultRealm, build_opener
2from urllib.error import URLError
3from urllib import request,parse
4from urllib.request import ProxyHandler, build_opener
5import ssl
6import http.cookiejar, urllib.request
7
8ssl._create_default_https_context = ssl._create_unverified_context
9
10
11‘‘‘验证‘‘‘
12def studyAuth():
13    username = ‘username‘
14    password = ‘password‘
15    url = ‘http://localhost:5000/‘
16    p = HTTPPasswordMgrWithDefaultRealm()
17    p.add_password(None, url, username, password)
18    auth_handle = HTTPBasicAuthHandler(p)
19    opener = build_opener(auth_handle)
20
21    try:
22        result = opener.open(url)
23        html = result.read().decode(‘utf-8‘)
24        print(html)
25    except URLError as  e:
26        print(e.reason)
27
28studyAuth()
29
30‘‘‘代理‘‘‘
31def studyProxy():
32    proxy_handle = ProxyHandler({
33        ‘http‘: ‘http:127.0.0.1:9743‘,
34        ‘https‘: ‘https:127.0.0.1:9743‘
35    })
36    opener = build_opener(proxy_handle)
37    try:
38        response = opener.open(‘https://www.baidu.com‘)
39        print(response.read().decode(‘utf-8‘))
40    except URLError as e:
41        print(e.reason)
42
43studyProxy()
44
45‘‘‘cookie‘‘‘
46def studyCookie():
47    cookie = http.cookiejar.CookieJar()
48    handle = urllib.request.HTTPCookieProcessor(cookie)
49    opener = urllib.request.build_opener(handle)
50    response = opener.open(‘https://www.baidu.com‘)
51    for item in cookie:
52        print(item.name)
53        print(item.value)
54        print(item.name + ‘=‘ + item.value)
55
56def studyCookie1():
57    filename = ‘cookie.txt‘
58    cookie = http.cookiejar.LWPCookieJar(filename)
59#   cookie = http.cookiejar.MozillaCookieJar(filename)
60    handle = urllib.request.HTTPCookieProcessor(cookie)
61    opener = urllib.request.build_opener(handle)
62    response = opener.open(‘http://www.baidu.com‘)
63    cookie.save(ignore_discard=True, ignore_expires=True)
64
65‘‘‘读取利用生成的cookie文件‘‘‘
66def studyCookie2():
67    cookie = http.cookiejar.LWPCookieJar()
68    cookie.load(‘cookie.txt‘, ignore_expires=True, ignore_discard=True)
69    handle = urllib.request.HTTPCookieProcessor(cookie)
70    opener = urllib.request.build_opener(handle)
71    response = opener.open(‘http://www.baidu.com‘)
72    print(response.read().decode(‘utf-8‘))
73
74studyCookie2()

urllib库认证,代理,cookie

原文地址:https://www.cnblogs.com/gxj521test/p/10206519.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved