网页常见的两种请求方式get post

Python笔记本
491 0

代码

import socket
import urllib
from urllib import request
from urllib import parse

# url = 'http://www.baidu.com'
# response = request.urlopen(url, timeout=1)
# print(response.read().decode('utf-8'))

data = bytes(parse.urlencode({'word': 'hello'}), encoding='utf-8')

response = request.urlopen('http://httpbin.org/post', data=data)
print(response.read().decode('utf-8'))

response2 = request.urlopen('http://httpbin.org/get', timeout=1)
print(response2.read().decode('utf-8'))

print('---------------------------------')


try:
    response3 = request.urlopen('http://httpbin.org/get', timeout=0.1)
except urllib.error.URLError as e:
    if isinstance(e.reason, socket.timeout):
        print("TimeOut")

运行

最后更新 2020-04-06
评论 ( 0 )
评论已关闭