import urllib.requestresponse = urllib.request.urlopen("http://www.python.org")print(response.status) #获取响应码print(response.getheaders()) #获取响应头信息print(response.getheader("Server")) #获取响应头中Server的值print(response.getheader('Content-Type'))"""运行结果如下:200[('Server', 'nginx'), ('Content-Type', 'text/html; charset=utf-8'), ('X-Frame-Options', 'SAMEORIGIN'), ('x-xss-protection', '1; mode=block'), ('X-Clacks-Overhead', 'GNU Terry Pratchett'), ('Via', '1.1 varnish'), ('Content-Length', '50114'), ('Accept-Ranges', 'bytes'), ('Date', 'Wed, 21 Nov 2018 14:28:32 GMT'), ('Via', '1.1 varnish'), ('Age', '1610'), ('Connection', 'close'), ('X-Served-By', 'cache-iad2141-IAD, cache-lax8624-LAX'), ('X-Cache', 'HIT, HIT'), ('X-Cache-Hits', '4, 107'), ('X-Timer', 'S1542810512.220409,VS0,VE0'), ('Vary', 'Cookie'), ('Strict-Transport-Security', 'max-age=63072000; includeSubDomains')]nginxtext/html; charset=utf-8"""
import urllibdata = bytes(urllib.parse.urlencode({ "word":"hello"}), encoding="utf8")print(data) #将参数转换成bytes类型 b'word=hello'response = urllib.request.urlopen("http://httpbin.org/post", data=data)print(response.read().decode("utf-8"))import urllib.requestimport socketimport urllib.errortry: #timeout实现超时处理 response = urllib.request.urlopen("http://httpbin.org/get", timeout=0.1)except urllib.error.URLError as e: if isinstance(e.reason, socket.timeout): #isinstance判断异常对象是不是时间类型 print("TIME OUT")import urllibrequest = urllib.request.Request("http://python.org")response = urllib.request.urlopen(request)print(response.read().decode("utf-8"))