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

代码

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")

运行

文章名: 《网页常见的两种请求方式get post》

本文链接:https://lula.fun/1024.html

除特别注明外,文章均由 Lula(噜啦) 原创

 原创文章 转载时请注明 出处 以及文章链接
最后修改:2019 年 10 月 09 日 12 : 05 PM

发表评论