为什么浏览器加倍请求后端

时间:2018-04-15 20:12:25

标签: python google-chrome http firefox opera

我在日志中注意到我的后端服务超载了很多超时请求。

因此我在Python中编写了一个简单的例子(非常容易编写和测试):

import socket
import threading

class RequestHandler(threading.Thread):
    def __init__(self, client, address):
        self.client = client
        self.client.settimeout(5.0)
        self.address = address
        threading.Thread.__init__(self, name="Handler")
        self.setDaemon(True)
        self.start()

    def run(self):
        print "Request: %s" % str(self.address)
        print "%s" % self.client.recv(1024)
        file = open("/Users/kk/Desktop/apachi-icon.png")
        data = file.read()
        file.close()
        self.client.send("HTTP/1.1 200 OK\r\n")
        self.client.send("Server: KkHttp/1.0.0\r\n")
        self.client.send("Cache-Control: max-age=%s\r\n" % (60 * 60 * 24))
        self.client.send("Content-Length: %d\r\n" % len(data))
        self.client.send("Connection: close\r\n")
        self.client.send("Content-Type: image/png\r\n")
        self.client.send("\r\n")
        self.client.send(data)
        self.client.close()
        print "Response: %s" % str(self.address)

acceptSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
acceptSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
acceptSocket.bind(("", 8080))
acceptSocket.listen(100)

while True:
    s, a = acceptSocket.accept()
    RequestHandler(s, a)

然后我使用我的Chrome(我测试过的Opera和Firefox也在做同样的事情)来请求一个文件:

Chrome

在我的Python服务器的控制台中,我可以看到两个请求,其中一个实际上是超时的:

Request: ('127.0.0.1', 61047)
GET /www/sportovniservis/frontend/img/apachi-icon.png HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: cs-CZ,cs;q=0.9,en;q=0.8
Cookie: __KK_BLAZEK_LOGIN__=%22789c0dd24b76a3381400d005f5a020b61318d440127a885f82240448333e061c619c3ea60ba556dfd9c11d5c69cf31f2eb35c6be9e243720bca41726e97dfb4d9552a21d1546f4830bfb81f059517f50133a30ae8b2b52450cd1e5f1a18ad70e85a3f1f58051b850963e15be14b27aefab4623c3e160c7de5d7930a587a2bd7023cc0be039add0296990878bdeee44ce1aa0f9124cacff142eb16c4606bccbd65a738b97e4c7c999da9e25fc15df8055998a20e72da4b5b8f8f11632211fbfb882265ace523a7d8d6263b3fb03750ed3ce3f8856aaaeb8af881b03def81bc6c315c08bc85ae44a8a13822126767f1442bfc5a7c2d05a4f233a1eb0f87b2cce5a56e1500
Request: ('127.0.0.1', 61048)
Response: ('127.0.0.1', 61047)
Exception in thread Handler:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/Users/kk/Library/Preferences/IntelliJIdea2018.1/scratches/scratch_18.py", line 16, in run
    print "%s" % self.client.recv(1024)
timeout: timed out

很明显,浏览器会立即(几乎并行)发送此请求,而不是重复上一次错误。

我正在谷歌上搜索,以找出这种非常奇怪的不当行为的原因,但没有任何成功。

任何人都可以解释发生了什么吗?

在Opera中BTW,更糟糕的是 - Opera不仅重复请求,而且还发送了很多次(Chrome只发送一次很好):

Request: ('127.0.0.1', 64403)
Request: ('127.0.0.1', 64405)
Request: ('127.0.0.1', 64410)
 Request: ('127.0.0.1', 64411)
Request: ('127.0.0.1', 64412)
Request: ('127.0.0.1', 64413)GET /www/sportovniservis/frontend/img/apachi-icon.png HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.168 Safari/537.36 OPR/51.0.2830.40
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: cs-CZ,cs;q=0.9
Cookie: Idea-6ce73599=05ec92cb-34ac-4c05-bc63-a15229bcfee3; Idea-6ce7359a=139c0d60-88fe-40cc-ae76-ce6c84147547



Response: ('127.0.0.1', 64403)
Exception in thread Handler:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/Users/kk/Library/Preferences/IntelliJIdea2018.1/scratches/scratch_18.py", line 16, in run
    print "%s" % self.client.recv(1024)
timeout: timed out

Exception in thread Handler:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/Users/kk/Library/Preferences/IntelliJIdea2018.1/scratches/scratch_18.py", line 16, in run
    print "%s" % self.client.recv(1024)
timeout: timed out

Exception in thread Handler:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/Users/kk/Library/Preferences/IntelliJIdea2018.1/scratches/scratch_18.py", line 16, in run
    print "%s" % self.client.recv(1024)
timeout: timed out

Exception in thread Handler:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/Users/kk/Library/Preferences/IntelliJIdea2018.1/scratches/scratch_18.py", line 16, in run
    print "%s" % self.client.recv(1024)
timeout: timed out

Exception in thread Handler:
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/Users/kk/Library/Preferences/IntelliJIdea2018.1/scratches/scratch_18.py", line 16, in run
    print "%s" % self.client.recv(1024)
timeout: timed out

0 个答案:

没有答案