简体   繁体   中英

python socket and epoll

I use python's socket and epoll to make a web server. My operating system is Linux CentOS 6, My python version is python 2.7.8. My source code is:

# -*-coding:utf-8-*-

import socket
import select
import time

EOL1 = b'\n\n'
EOL2 = b'\n\r\n'
response = b'HTTP/1.0 200 OK\r\nDate: Mon, 1 Jan 1996 01:01:01 GMT\r\n'
response += b'Content-Type: text/plain\r\nContent-Length: 13\r\n\r\n'
response += b'<html><head><title>title</title></head><body><p>Hello, world!</p></body></html>'

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serversocket.bind(('0.0.0.0', 8080))
serversocket.listen(1)  # the number of client that connect to server
serversocket.setblocking(0)  # set 0 not block other block
serversocket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

epoll = select.epoll()
epoll.register(serversocket.fileno(), select.EPOLLIN)

try:
    connections = {}
    requests = {}
    responses = {}
    while True:
        events = epoll.poll(1)
        for fileno, event in events:
            if fileno == serversocket.fileno():  # if request come
                connection, address = serversocket.accept()  # waiting income connection
                connection.setblocking(0)  # none block
                epoll.register(connection.fileno(), select.EPOLLIN)  # register socket read event to epoll
                connections[connection.fileno()] = connection  # add connection to connections dict
                requests[connection.fileno()] = b''
                responses[connection.fileno()] = response  # write data to responses dict
            elif event & select.EPOLLIN:  # when data in os's read buffer area
                requests[fileno] += connections[fileno].recv(1024)  # read data from connections
                if EOL1 in requests[fileno] or EOL2 in requests[fileno]:  # if http message
                    print('-' * 40 + '\n' + requests[fileno].decode()[:-2])
                    responses[fileno] += str(time.time())
                    epoll.modify(fileno, select.EPOLLOUT)  # change file number to epoll out mode
            elif event & select.EPOLLOUT:  # if out mode
                byteswritten = connections[fileno].send(responses[fileno])  # write data to os's write buffer
                responses[fileno] = responses[fileno][byteswritten:]  # get http response message
                if len(responses[fileno]) == 0:  # if file sent
                    epoll.modify(fileno, 0)  # change file number to hup mode
                    connections[fileno].shutdown(socket.SHUT_RDWR)  # set socket read and write mode shutdown
            elif event & select.EPOLLHUP:  # if message sent and file number in epoll is hup
                epoll.unregister(fileno)  # remove file number from epoll
                connections[fileno].close()  # close connection
                del connections[fileno]  # delete connection from connections dict
finally:
    epoll.unregister(serversocket.fileno())
    epoll.close()
    serversocket.close()

But when I open web browser and visit " http://localhost:8080/ ", I get some data like these <html><head>< ,it is not full data, it just a part of my data.What's the matter in my project. view more info please look this picture. 在此处输入图片说明

您的代码中包含“ Content-Length:13”,因此仅显示前13个字符!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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