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

1.Timeout设置超时

只能修改Socket设置全局Timeout

#! /usr/bin/env python3

import socket

import urllib.request

# timeout in seconds

timeout = 2

socket.setdefaulttimeout(timeout)

# this call to urllib.request.urlopen now uses the default timeout

# we have set in the socket module

req = urllib.request.Request('http://www.python.org/')

a = urllib.request.urlopen(req).read()

print(a)

2.获取HTTP响应码

#! /usr/bin/env python3

import urllib.request

req = 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)

转载于:https://www.cnblogs.com/mrwuzs/p/8018303.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: HTTP Error 504: Gateway Time-out是一个常见的错误,表示网关超时。这通常是由于服务器在请求的时间内没有收到响应而导致的。解决这个问题的方法有几种。引用\[1\]中提到的第一种方法是为urlopen设置timeout参数,这样可以在请求超时时抛出异常,避免程序卡死。另一种方法是使用retrying库,通过设置重试次数和重试间隔来处理异常,如引用\[2\]所示。这样可以在出现异常时自动重试请求,提高程序的稳定性。所以,你可以尝试在你的代中使用这些方法来解决HTTP Error 504: Gateway Time-out的问题。 #### 引用[.reference_title] - *1* *2* [Python爬取网页动态数据出现urllib.error.HTTPError: HTTP Error 504: Gateway Time-out问题](https://blog.csdn.net/sfwqwfew/article/details/127880014)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Python学习笔记(二)urllib.urlopen()超时问题 : 504Gateway Time-out](https://blog.csdn.net/m0_37374307/article/details/80326715)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值