分享web开发知识

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

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

urllib的实现---timeout,获取http响应码,重定向,proxy的设置

发布时间:2023-09-06 01:29责任编辑:蔡小小关键词:urlhttp

1.Timeout设置超时

只能修改Socket设置全局Timeout

#! /usr/bin/env python3import socketimport urllib.request# timeout in secondstimeout = 2socket.setdefaulttimeout(timeout)# this call to urllib.request.urlopen now uses the default timeout# we have set in the socket modulereq = urllib.request.Request(‘http://www.python.org/‘)a = urllib.request.urlopen(req).read()print(a)

2.获取HTTP响应码

#! /usr/bin/env python3import urllib.requestreq = urllib.request.Request(‘http://python.org/‘)try: ?  urllib.request.urlopen(req)except urllib.error.HTTPError as e:  print(e.code)print(e.read().decode("utf8"))

3、异常处理1

 1 #! /usr/bin/env python3 2 ?3 from urllib.request import Request, urlopen 4 ?5 from urllib.error import URLError, HTTPError 6 ?7 req = Request(‘http://www.python.org/‘) 8 ?9 try:10 11   response = urlopen(req)12 13 except HTTPError as e:14 15   print(‘The (www.python.org)server couldn‘t fulfill the request.‘)16 17   print(‘Error code: ‘, e.code)18 19 except URLError as e:20 21   print(‘We failed to reach a server.‘)22 23   print(‘Reason: ‘, e.reason)24 25 else:26 27   print("good!")28 29   print(response.read().decode("utf8")) 

4、异常处理2

 1 #! /usr/bin/env python3 2 ?3 from urllib.request import Request, urlopen 4 ?5 from urllib.error import ?URLError 6 ?7 req = Request("http://www.python.org/") 8 ?9 try:10 11   response = urlopen(req)12 13 except URLError as e:14 15   if hasattr(e, ‘reason‘):16 17     print(‘We failed to reach a server.‘)18 19     print(‘Reason: ‘, e.reason)20 21   elif hasattr(e, ‘code‘):22 23     print(‘The server couldn‘t fulfill the request.‘)24 25     print(‘Error code: ‘, e.code)26 27 else: ?print("good!")28 29   print(response.read().decode("utf8"))

5.重定向

import urllib.request
import socket
url = ‘https://www.baidu.com‘
response =urllib.request.urlopen(url)
isRediercted = response.geturl() == "https://www.baidu.com"


6.代理设置

import urllib.request

proxy_support = urllib.request.ProxyHandler({‘sock5‘: ‘localhost:1080‘})

opener = urllib.request.build_opener(proxy_support)

urllib.request.install_opener(opener)

a = urllib.request.urlopen("http://www.python.org/").read().decode("utf8")

print(a)

urllib的实现---timeout,获取http响应码,重定向,proxy的设置

原文地址:http://www.cnblogs.com/mrwuzs/p/8018303.html

知识推荐

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