Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 字典访问时出现KeyError,但仍返回正确的值?_Python_Http_Response_Keyerror - Fatal编程技术网

Python 字典访问时出现KeyError,但仍返回正确的值?

Python 字典访问时出现KeyError,但仍返回正确的值?,python,http,response,keyerror,Python,Http,Response,Keyerror,因此,Python目前的行为相当怪异。我编写了一个非常简单的服务器,用于返回HTTP GET参数“url”的值。就行为而言,这符合预期;服务器使用键“url”检索参数并返回其值。但是,在输出时,Python解释器抛出一个keyrerror,表示键“url”不存在 File "/Users/me/Desktop/workspace/backend/ServerHandler.py", line 12, in getURL return dictionary['url'] KeyErro

因此,Python目前的行为相当怪异。我编写了一个非常简单的服务器,用于返回HTTP GET参数“url”的值。就行为而言,这符合预期;服务器使用键“url”检索参数并返回其值。但是,在输出时,Python解释器抛出一个keyrerror,表示键“url”不存在

  File "/Users/me/Desktop/workspace/backend/ServerHandler.py", line 12, in getURL
  return dictionary['url']
  KeyError: 'url'
代码如下所示:

def dictify(self, full_path):
    return urlparse.parse_qs(urlparse.urlparse(full_path).query)

def getURL(self, full_path):
    dictionary = self.dictify(full_path)
    return dictionary['url']

def do_GET(self):
    self.send_response(200)
    self.send_header("Content-type", "text/html")
    self.end_headers()
    self.wfile.write(self.getURL(self.path))
请求如下:

然而,正如预期的那样,结果是正确的


虽然结果正确令人高兴,但我仍然不明白为什么会发生这种情况。有什么想法吗

你为什么不在return语句之前打印字典,看看里面有什么?@alfasin刚刚弄明白了。Google Chrome bug的一部分,由于favicon事件,它在页面加载时向同一服务器发送两个HTTP请求。。。