使用urllib.urlopen()时,我们会使用getcode()方法来接受Https的状态返回码,但是有时返回200,有时返回201,都是正确的,到底是如何工作的呢?
def urlopen(url, data=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
*, cafile=None, capath=None, cadefault=False, context=None):
def get_method(self):
"""Return a string indicating the HTTP request method."""
default_method = "POST" if self.data is not None else "GET"
return getattr(self, 'method', default_method)
找到源码如上所示,
当urlopen()接收data参数接收为None是则使用的是GET请求,而GET请求的HTTPS状态码成功的状态码为200。
当urlopen()接收data参数接收不为None是则使用的是POST请求,而POST请求的HTTPS状态码成功的状态码为201。