Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python(自定义web服务器)中解析HTTP请求_Python_Http_Request - Fatal编程技术网

如何在python(自定义web服务器)中解析HTTP请求

如何在python(自定义web服务器)中解析HTTP请求,python,http,request,Python,Http,Request,我正在尝试用python创建一个多线程web服务器。我已经设法让它与基本文本一起工作,但我在适应HTTP方面遇到了困难 它在发送GET请求时也会崩溃,但在仅使用“connect”时不会崩溃。我是否没有在localhost:port上正确设置服务器 然而,主要的问题是我有一个客户端套接字,但我不知道如何从中提取请求数据 #! /usr/bin/env python3.3 import socket, threading, time, sys, queue #initial number of w

我正在尝试用python创建一个多线程web服务器。我已经设法让它与基本文本一起工作,但我在适应HTTP方面遇到了困难

它在发送GET请求时也会崩溃,但在仅使用“connect”时不会崩溃。我是否没有在localhost:port上正确设置服务器

然而,主要的问题是我有一个客户端套接字,但我不知道如何从中提取请求数据

#! /usr/bin/env python3.3
import socket, threading, time, sys, queue

#initial number of worker threads
kNumThreads = 50

#queue of all the incoming requests
connections = queue.Queue(-1)


class Request_Handler(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        while(True):
            #Get the request socket data from the queue
            global connections
            self.connection = connections.get()
            self.addr = self.connection[1]
            self.socket = self.connection[0]

            #I have the socket, but how do I parse the request from here?
            #Also is there a better way to respond than to manually create a response here?

            self.response = 'HTTP/1.1 200 OK/nContent-Type: text/html; charset=UTF-8/n/n <html><body>Hello World</body></html>'
            print("got here")
            self.socket.send(self.response.encode('utf-8'))
            self.socket.close()
            requests.task_done()


#creates kNumThreads threads that will handle requests
def create_threads():
    for n in range(0, kNumThreads):
       RH = Request_Handler()
       RH.start()

def main():
    s = socket.socket()
    port = int(sys.argv[1])

    #sets up the server locally
    s.bind(('127.0.0.1', port))
    s.listen(100)

    #create the worker threads   
    create_threads()

    #accept connections and add them to queue
    while(True):
        c, addr = s.accept()
        connections.put_nowait((c, addr))

if __name__ == '__main__':
    main()
#/usr/bin/env蟒蛇3.3
导入套接字、线程、时间、系统、队列
#工作线程的初始数目
kNumThreads=50
#所有传入请求的队列
连接=队列。队列(-1)
类请求\u处理程序(threading.Thread):
定义初始化(自):
threading.Thread.\uuuuu init\uuuuuu(自)
def运行(自):
虽然(正确):
#从队列中获取请求套接字数据
全局连接
self.connection=connections.get()
self.addr=self.connection[1]
self.socket=self.connection[0]
#我有套接字,但是如何从这里解析请求?
#还有比在这里手动创建响应更好的响应方式吗?
self.response='HTTP/1.1200ok/n内容类型:text/html;字符集=UTF-8/n/n Hello World'
打印(“到达这里”)
self.socket.send(self.response.encode('utf-8'))
self.socket.close()
请求。任务完成()
#创建将处理请求的kNumThreads线程
def create_threads():
对于范围内的n(0,kNumThreads):
RH=请求处理程序()
右开始()
def main():
s=socket.socket()
port=int(sys.argv[1])
#在本地设置服务器
s、 绑定(('127.0.0.1',端口))
s、 听(100)
#创建工作线程
创建_线程()
#接受连接并将其添加到队列
虽然(正确):
c、 addr=s.accept()
连接。放置(c,地址))
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

您需要从以下连接接收数据:

while(True):
    c, addr = s.accept()
    data = c.recv(1024)
    print(data)
    connections.put_nowait((c, addr))
关于如何读取请求,请参阅HTTP规范


是的,但我认为requests模块仅用于发出请求,而不用于解析them@SrinivasReddyThatiparthy. 起首部分,真的。很少有人在不生气的情况下接受纠正和批评。尤其是在论坛上。起首部分。@Hyperboreus道歉。我的英语很差。我认为它的意思是相反的。刚才在谷歌上搜索并找到了它。好吧,我想我可以手动解析它,但我想知道是否有一个很好的库用于此