处理服务器端的持久请求

时间:2019-09-30 10:14:15

标签: python sockets websocket tcpserver

当前我的代码是这样的:

class HttpRequest:

    method = ""
    path = ""
    key = ""
    gotContentBody = False
    contentBody = ""
    contentLen = 0

    def __init__(self, httpRequestString):
        #Assume request always in right format
        partition = httpRequestString.split()

        header = partition[1].split('/')
        if header[1] == 'key':
            self.key = header[2]

        if len(partition):
            self.method=partition[0].upper()
            self.path=partition[1]
            for contentCount in range(2,len(partition),2):
                if partition[contentCount].lower()=='content-length':
                    getContent = True
                    contentLen = int(partition[contentCount+1])
                    break

它可以像

一样处理单个httpRequest
POST /key/key content-length 7  abcdefg
    print(foo)

但是,我想发送多个请求,例如:

POST /key/key content-length 7  abcdefgPOST /key/key content-length 7  gfedcbaGET /key/key  

它包含三个请求:POST,POST和GET

我还需要将httpRequestString传递给其他函数,该怎么办?

0 个答案:

没有答案