如何仅使用套接字库通过 Python 正确发送 HTTP 响应?

Posted

技术标签:

【中文标题】如何仅使用套接字库通过 Python 正确发送 HTTP 响应?【英文标题】:How to properly send HTTP response with Python using socket library only? 【发布时间】:2012-04-24 05:55:50 【问题描述】:

我有一个用 Python 编写的非常简单的网络服务器。它监听 13000 端口,如果在浏览器中打开 http://localhost:13000,我怎样才能让它提供一个简单的“Hello World”网页?

这就是我的代码:

# set up socket and connection
while True:
    sock, addr = servSock.accept()
    # WHAT GOES HERE?
    sock.close()

如您所见,我不确定如何实际发回网页?

我只需要使用socket 库。

编辑:问题不在于我不知道如何制定 HTTP 响应,我不知道如何真正让它显示在我的浏览器中!它只是保持旋转/加载。

【问题讨论】:

【参考方案1】:
# set up socket and connection
while True:
    sock, addr = servSock.accept()
    sock.send("""HTTP/1.1 200 OK
Content-Type: text/html

<html><body>Hello World</body></html>
""");
    sock.close()

【讨论】:

【参考方案2】:

更新到其中一种解决方案,因为最新版本要求以字节格式发送数据

while True:
    sock, addr = servSock.accept()
    sock.sendall(b"HTTP/1.1 200 OK\n"
         +b"Content-Type: text/html\n"
         +b"\n" # Important!
         +b"<html><body>Hello World</body></html>\n");
    sock.shutdown(soket.SHUT_WR)
    sock.close()

我本可以编辑上面的帖子,但队列已满 :(. 也可以使用encode() 方法转换为字节fromat。

【讨论】:

【参考方案3】:

我接受了先前的答案并编辑了 Python3 utf-8 和字节编码的代码。感谢您的原始答案,它帮了很多忙。

import socket

MAX_PACKET = 32768

def recv_all(sock):
    r'''Receive everything from `sock`, until timeout occurs, meaning sender
    is exhausted, return result as string.'''

    # dirty hack to simplify this stuff - you should really use zero timeout,
    # deal with async socket and implement finite automata to handle incoming data

    prev_timeout = sock.gettimeout()
    try:
        sock.settimeout(0.1)

        rdata = []
        while True:
            try:
                # Gotta watch for the bytes and utf-8 encoding in Py3
                rdata.append(sock.recv(MAX_PACKET).decode('utf-8')) 
            except socket.timeout:
                return ''.join(rdata)

        # unreachable
    finally:
        sock.settimeout(prev_timeout)

def normalize_line_endings(s):
    r'''Convert string containing various line endings like \n, \r or \r\n,
    to uniform \n.'''
    test = s.splitlines()
    return ''.join((line + '\n') for line in s.splitlines())

def run():
    r'''Main loop'''

    # Create TCP socket listening on 10000 port for all connections,
    # with connection queue of length 1
    server_sock = socket.socket(socket.AF_INET,
                                socket.SOCK_STREAM,
                                socket.IPPROTO_TCP)
    #Added the port 13001 for debuging purposes 

    try:
        server_sock.bind(('0.0.0.0', 13000))
        print('PORT 13000')
    except:
        server_sock.bind(('0.0.0.0', 13001))
        print('PORT 13001')
    # except:
    #     server_sock.bind(('0.0.0.0', 13002))
    #     print('PORT 13002')

    server_sock.listen(1)

    while True:
        # accept connection
        try:
            client_sock, client_addr = server_sock.accept()

            # headers and body are divided with \n\n (or \r\n\r\n - that's why we
            # normalize endings). In real application usage, you should handle
            # all variations of line endings not to screw request body
            request = normalize_line_endings(recv_all(client_sock)) # hack again

            request_head, request_body = request.split('\n\n', 1)

            # first line is request headline, and others are headers
            request_head = request_head.splitlines()
            request_headline = request_head[0]
            # headers have their name up to first ': '. In real world uses, they
            # could duplicate, and dict drops duplicates by default, so
            # be aware of this.
            request_headers = dict(x.split(': ', 1) for x in request_head[1:])

            # headline has form of "POST /can/i/haz/requests HTTP/1.0"
            request_method, request_uri, request_proto = request_headline.split(' ', 3)

            response_body = [
                '<html><body><h1 style="color:red">Hello, world!</h1>',
                '<p>This page is in location %(request_uri)r, was requested ' % locals(),
                'using %(request_method)r, and with %(request_proto)r.</p>' % locals(),
                '<p>Request body is %(request_body)r</p>' % locals(),
                '<p>Actual set of headers received:</p>',
                '<ul>',
            ]

            for request_header_name, request_header_value in request_headers.items():
                response_body.append('<li><b>%r</b> == %r</li>' % (request_header_name,
                                                                    request_header_value))

            response_body.append('</ul></body></html>')

            response_body_raw = ''.join(response_body)

            # Clearly state that connection will be closed after this response,
            # and specify length of response body
            response_headers = 
                'Content-Type': 'text/html; encoding=utf8',
                'Content-Length': len(response_body_raw),
                'Connection': 'close',
            

            response_headers_raw = ''.join('%s: %s\n' % (k, v) for k, v in \
                                                    response_headers.items())

            # Reply as HTTP/1.1 server, saying "HTTP OK" (code 200).
            response_proto = 'HTTP/1.1'.encode()
            response_status = '200'.encode()
            response_status_text = 'OK'.encode() # this can be random

            # sending all this stuff
            client_sock.send(b'%s %s %s' % (response_proto, response_status,
                                                            response_status_text))
            client_sock.send(response_headers_raw.encode())
            client_sock.send(b'\n') # to separate headers from body
            client_sock.send(response_body_raw.encode())

            # and closing connection, as we stated before

        finally:
            client_sock.close()

run()

【讨论】:

【参考方案4】:

您可能想要检查 Web 对象 http://www.webob.org/

这是一个简单的轻量级项目,用于创建与 http 兼容的请求和响应。你可以对你的请求/响应对象做任何事情......或者只是将繁重的工作委托给 WebObjects

样本

>>> from webob import Response
>>> res = Response()
>>> res.status
'200 OK'
>>> res.headerlist
[('Content-Type', 'text/html; charset=UTF-8'), ('Content-Length', '0')]
>>> res.body
''

【讨论】:

【参考方案5】:

或者,如果您只是不想记住完整的协议,您可以使用以下方法再次找到它:

 % nc ***.com 80
GET / HTTP/1.1
Host: ***.com

HTTP/1.1 200 OK
Cache-Control: public, max-age=60
Content-Type: text/html; charset=utf-8
Expires: Wed, 11 Apr 2012 21:33:49 GMT
Last-Modified: Wed, 11 Apr 2012 21:32:49 GMT
Vary: *
Date: Wed, 11 Apr 2012 21:32:49 GMT
Content-Length: 206008

[...]
 % 

嗯,你通常会更喜欢一个比 *** 更简洁的网站(通常只提供静态文件);)

最低要求(您会在答案中找到)是:

sock.send(r'''HTTP/1.0 200 OK
Content-Type: text/plain

Hello, world!

''')

服务器必须两次返回才能获得答案,否则浏览器会无限期地等待标头

但是要模仿网络服务器的行为,不要忘记在浏览器向您发送一些数据之后发送您的答案,然后是两个回车,通常您可以使用以下方式获取它发送的内容:

 % nc -kl localhost 13000
GET / HTTP/1.1
Host: localhost:13000
User-Agent: Mozilla/5.0...
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive

 %

这样你就可以改进你的测试程序

【讨论】:

【参考方案6】:

根据问题变化更新

它可能一直在旋转,因为在缺少Content-LengthConnection 标头的情况下,浏览器可能会认为它是Connection: keep-alive,因此它会永远继续从您的服务器接收数据。尝试发送Connection: close,并传递实际的Content-Length 看看是否有帮助。


这不会达到你的预期吗? :)
#!/usr/bin/env python
# coding: utf8

import socket

MAX_PACKET = 32768

def recv_all(sock):
    r'''Receive everything from `sock`, until timeout occurs, meaning sender
    is exhausted, return result as string.'''

    # dirty hack to simplify this stuff - you should really use zero timeout,
    # deal with async socket and implement finite automata to handle incoming data

    prev_timeout = sock.gettimeout()
    try:
        sock.settimeout(0.01)

        rdata = []
        while True:
            try:
                rdata.append(sock.recv(MAX_PACKET))
            except socket.timeout:
                return ''.join(rdata)

        # unreachable
    finally:
        sock.settimeout(prev_timeout)

def normalize_line_endings(s):
    r'''Convert string containing various line endings like \n, \r or \r\n,
    to uniform \n.'''

    return ''.join((line + '\n') for line in s.splitlines())

def run():
    r'''Main loop'''

    # Create TCP socket listening on 10000 port for all connections, 
    # with connection queue of length 1
    server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, \
                                socket.IPPROTO_TCP)
    server_sock.bind(('0.0.0.0', 13000))
    server_sock.listen(1)

    while True:
        # accept connection
        client_sock, client_addr = server_sock.accept()

        # headers and body are divided with \n\n (or \r\n\r\n - that's why we
        # normalize endings). In real application usage, you should handle 
        # all variations of line endings not to screw request body
        request = normalize_line_endings(recv_all(client_sock)) # hack again
        request_head, request_body = request.split('\n\n', 1)

        # first line is request headline, and others are headers
        request_head = request_head.splitlines()
        request_headline = request_head[0]
        # headers have their name up to first ': '. In real world uses, they
        # could duplicate, and dict drops duplicates by default, so
        # be aware of this.
        request_headers = dict(x.split(': ', 1) for x in request_head[1:])

        # headline has form of "POST /can/i/haz/requests HTTP/1.0"
        request_method, request_uri, request_proto = request_headline.split(' ', 3)

        response_body = [
            '<html><body><h1>Hello, world!</h1>',
            '<p>This page is in location %(request_uri)r, was requested ' % locals(),
            'using %(request_method)r, and with %(request_proto)r.</p>' % locals(),
            '<p>Request body is %(request_body)r</p>' % locals(),
            '<p>Actual set of headers received:</p>',
            '<ul>',
        ]

        for request_header_name, request_header_value in request_headers.iteritems():
            response_body.append('<li><b>%r</b> == %r</li>' % (request_header_name, \
                                                    request_header_value))

        response_body.append('</ul></body></html>')

        response_body_raw = ''.join(response_body)

        # Clearly state that connection will be closed after this response,
        # and specify length of response body
        response_headers = 
            'Content-Type': 'text/html; encoding=utf8',
            'Content-Length': len(response_body_raw),
            'Connection': 'close',
        

        response_headers_raw = ''.join('%s: %s\n' % (k, v) for k, v in \
                                                response_headers.iteritems())

        # Reply as HTTP/1.1 server, saying "HTTP OK" (code 200).
        response_proto = 'HTTP/1.1'
        response_status = '200'
        response_status_text = 'OK' # this can be random

        # sending all this stuff
        client_sock.send('%s %s %s' % (response_proto, response_status, \
                                                        response_status_text))
        client_sock.send(response_headers_raw)
        client_sock.send('\n') # to separate headers from body
        client_sock.send(response_body_raw)

        # and closing connection, as we stated before
        client_sock.close()

run()

更详细的说明请见description of HTTP protocol。

【讨论】:

是的,但是……浏览器一直在“旋转”,什么也没有显示? 我已经更新了代码示例以保证 100% 工作 :) 我希望您会发现标头处理的基本原则很有用,但我建议您不要依赖此类代码并实现全功能 HTTP请求解析器。 好吧,你已经给出了一个非常完整的答案......虽然我认为它仍在旋转(直到超时)的原因是它正在等待双“\ n”。但至少,您的代码示例是一个很好的 sn-p,以防万一;) 谢谢。我认为\nresponse_headers_raw 之前被省略了。 我知道这已经很晚了,但我已经复制了你的代码,但它对我不起作用。我认为问题可能出在请求上。 fetch("IP:PORT")也是如此。IP是服务器的公网ip?【参考方案7】:

发回类似的东西:

HTTP/1.1 200 OK
Date: Wed, 11 Apr 2012 21:29:04 GMT
Server: Python/6.6.6 (custom)
Content-Type: text/html

然后是实际的 html 代码。确保在 Content-Type 行之后和 html 之前有一个换行符。

【讨论】:

以上是关于如何仅使用套接字库通过 Python 正确发送 HTTP 响应?的主要内容,如果未能解决你的问题,请参考以下文章

C++ 如何使用 Socket 类向 HTTP 服务器发送数据和接收响应

windows下socket编程小例子

简单socket服务器的基本理解

win 10 UAP/UWP(通用应用程序)的异步套接字库?

Python 套接字库认为套接字在未打开时已打开

如何使 LAN 客户端可以发现服务器