python套接字获取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34192093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:34:53  来源:igfitidea点击:

python socket GET

pythonpython-sockets

提问by james smith

From the other posts on stack overflow this should be working

从堆栈溢出的其他帖子中,这应该有效

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)                 

s.connect(("www.cnn.com" , 80))
s.sendall("GET / HTTP/1.1\r\n")
print s.recv(4096)
s.close

but for some reason it just hangs (at recv) and never prints. I know that a request to www.cnn.com will chunk it's data but I should at least read something from recv, right?

但出于某种原因,它只是挂起 (at recv) 并且从不打印。我知道对 www.cnn.com 的请求会将其数据分块,但我至少应该从中读取一些内容recv,对吗?

p.s. I know this isn't the best way to do it and that there are library like httpliband urllib2out there, but I can't use those for this project (it's for school). I have to use the socketlibrary

PS我知道这是不是做到这一点的最好办法,而且有像库httpliburllib2在那里,但我不能使用那些为这个项目(这是学校)。我必须使用socket图书馆

采纳答案by Takis

You forgot to send a blank line after your request line:

您忘记在请求行后发送一个空行:

s.sendall("GET / HTTP/1.1\r\n\r\n")

Furthermore, HTTP 1.1 specifies you should add the Hostheader field as documented in the Host section in the HTTP 1.1 RFC.

此外,HTTP 1.1 指定您应该添加Host标头字段,如HTTP 1.1 RFCHost 部分中所述

s.sendall("GET / HTTP/1.1\r\nHost: www.cnn.com\r\n\r\n")

回答by Kevin Guan

Try replace this line:

尝试替换此行:

s.sendall("GET / HTTP/1.1\r\n")

with:

和:

s.sendall("GET / HTTP/1.1\r\n\r\n")
                             ^^^^

Also, I think you need replace s.closewith s.close()since it's a function.

另外,我认为您需要替换为s.closes.close()因为它是一个函数。

回答by mhawke

Your code is almost right, but you need to send 2 \r\nsequences to satisfy the HTTP protocol.

您的代码几乎是正确的,但是您需要发送 2 个\r\n序列来满足 HTTP 协议。

A valid GET request will look like this (note 2 lines):

有效的 GET 请求如下所示(注意 2 行):

GET / HTTP/1.1

So your code should be:

所以你的代码应该是:

s.sendall('GET / HTTP/1.1\r\n\r\n')

Further to that, there are additional headers required for valid HTTP 1.1 requests, such as Host:. You need to add them to your request, something like this:

此外,有效的 HTTP 1.1 请求还需要其他标头,例如Host:. 您需要将它们添加到您的请求中,如下所示:

s.sendall('''GET / HTTP/1.1
Host: cnn.com

''')

回答by james smith

Sorry to waste everyone's time. I just found this solution hereon Stack Overflow (just took some rewording in my Google search to find)

抱歉耽误了大家的时间。我刚刚发现这个解决方案在这里对堆栈溢出(只是把我在谷歌的一些措词搜索找到)

import socket
request = b"GET / HTTP/1.1\nHost: www.cnn.com\n\n"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("cnn.com", 80))
s.send(request)
result = s.recv(10000)
while (len(result) > 0):
    print(result)
    result = s.recv(10000)

And all of the answers were right as well about the ending \r\n\r\nhowever those returned 301statuses. This solution seems to follow the redirect somehow? Anyways, this solutions worked for me

所有关于结局的答案也是正确的,\r\n\r\n但是那些返回的301状态。这个解决方案似乎以某种方式遵循重定向?无论如何,这个解决方案对我有用

回答by Jan Bodnar

I am cleaning up the examples for Python 3. We need bytes/string conversion and we can also use automatic closing of the connection using with:

我正在清理 Python 3 的示例。我们需要字节/字符串转换,我们还可以使用以下命令自动关闭连接with

#!/usr/bin/env python3

import socket

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:

    s.connect(("example.com" , 80))
    s.sendall(b"GET / HTTP/1.1\r\nHost: example.com\r\nAccept: text/html\r\n\r\n")
    print(str(s.recv(4096), 'utf-8'))

回答by sibi

@james: you did a SlowLoris attack there without aware of it. I can't explain better than here, https://www.youtube.com/watch?v=XiFkyR35v2YI assumed that you found the solution from all the above answers but I just answered to bring this to your knowledge. :)

@james:您在那里进行了 SlowLoris 攻击而没有意识到这一点。我无法解释比这里更好的了,https://www.youtube.com/watch?v=XiFkyR35v2Y我假设你从上述所有答案中找到了解决方案,但我只是回答让你知道。:)