使用套接字创建原始HTTP请求

问题描述:

我希望能够构造一个原始HTTP请求并使用套接字发送它。显然,你希望我使用urllib和urllib2之类的东西,但我不想使用它。

I would like to be able to construct a raw HTTP request and send it with a socket. Obviously, you would like me to use something like urllib and urllib2 but I do not want to use that.

它必须看起来像这样:

import socket

tcpsoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsoc.bind(('72.14.192.58', 80)) #bind to googles ip
tcpsoc.send('HTTP REQUEST')
response = tcpsoc.recv()

显然你还需要请求页面/文件并获取和发布参数

Obviously you would also have to request the page/file and get and post parameters

您需要知道的大部分内容都在HTTP / 1.1规范中,如果您想要自己编写HTTP实现,那么您应该学习它: http://www.w3.org/Protocols/rfc2616/rfc2616.html

Most of what you need to know is in the HTTP/1.1 spec, which you should definitely study if you want to roll your own HTTP implementation: http://www.w3.org/Protocols/rfc2616/rfc2616.html