python套接字GET

编程入门 行业动态 更新时间:2024-10-27 13:21:29
本文介绍了python套接字GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在堆栈溢出的其他帖子中,这应该可以正常工作

From the other posts on stack overflow this should be working

import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("wwwn" , 80)) s.sendall("GET / HTTP/1.1\r\n") print s.recv(4096) s.close

,但是由于某种原因,它只是挂起(在recv)并且从不打印.我知道对wwwn的请求将对数据进行分块处理,但是我至少应该从recv中读取某些内容,对吧?

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

p.s.我知道这不是最好的方法,并且那里有类似httplib和urllib2的库,但是我不能将它们用于该项目(用于学校).我必须使用socket库

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

推荐答案

您忘了在请求行之后发送空白行:

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

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

此外,HTTP 1.1指定您应添加Host标头字段rel ="noreferrer"> HTTP 1.1 RFC中的主机部分.

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

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

更多推荐

python套接字GET

本文发布于:2023-08-07 03:51:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316813.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!