简体   繁体   English

循环运行时间(真的很慢)

[英]Running time in a while loop (really slow)

Following is my code: 以下是我的代码:

import socket
import time
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send(b'GET /code/romeo.txt HTTP/1.1\n')
mysock.send(b'Host: www.py4inf.com\n\n')
all = b""

while True:
    data = mysock.recv(512)
    all = all + data
    if len(data) < 1:
        break

mysock.close()

stuff = all.decode()
position = stuff.find('\r\n\r\n')
print(stuff[position+4:])

There must be something wrong because it takes almost 30 seconds to invoke break in while loop. 一定有问题,因为在while循环中调用break需要30秒钟。 However, if I change the code if len(data) < 1: to if len(data) < 100: it took just 0.5 second. 但是,如果我将if len(data) < 1:更改为if len(data) < 100:更改代码仅花了0.5秒。

Please help. 请帮忙。 It haunted me for a while. 它困扰了我一段时间。 The sample website: http://www.py4inf.com/code/romeo.txt 示例网站: http : //www.py4inf.com/code/romeo.txt

Web servers don't have to close connections immediately.In fact, they may be looking for another http request. Web服务器不必立即关闭连接。实际上,它们可能正在寻找另一个http请求。 Just add print(data) after the recv and you'll see you get the data, then a pause, then b'' , meaning the server finally closed the socket. 只需在recv之后添加print(data) ,您将看到获取数据,然后是暂停,然后是b'' ,这意味着服务器最终关闭了套接字。

You'll also notice that the server sends a header that includes "Content-Length: 167\\r\\n". 您还将注意到服务器发送了一个包含“ Content-Length:167 \\ r \\ n”的标头。 Once the header has finished, the server will send exactly 167 bytes of data. 标头完成后,服务器将准确发送167个字节的数据。 You could parse out the header yourself but that's why we have client libraries like urllib and requests . 您可以自己解析标头,这就是为什么我们拥有urllibrequests等客户端库的原因。

I was curious about how much would need to be added to the request header to get the connection to close immediately, and Connection: close seemed to do it. 我很好奇,需要多少添加到请求标头才能立即关闭连接,而Connection: close似乎可以做到。 This returns right away: 这立即返回:

import socket
import time
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('www.py4inf.com', 80))
mysock.send(b'GET /code/romeo.txt HTTP/1.1\n')
mysock.send(b'Connection: close\n')
mysock.send(b'Host: www.py4inf.com\n\n')
all = b""

while True:
    data = mysock.recv(512)
    all = all + data
    if len(data) < 1:
        break

mysock.close()

stuff = all.decode()
position = stuff.find('\r\n\r\n')
print(stuff[position+4:])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM