标签: basehttpserver

如何静默/安静HTTPServer和BasicHTTPRequestHandler的stderr输出?

我正在编写一个简单的http服务器作为我项目的一部分.下面是我的脚本的框架:

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

class MyHanlder(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write('<html><body><p>OK</p></body></html>')

httpd = HTTPServer(('', 8001), MyHanlder)
httpd.serve_forever()
Run Code Online (Sandbox Code Playgroud)

我的问题:每次客户端连接到我的服务器时,如何抑制脚本生成的stderr日志输出?

我已经查看了HTTPServer类到它的父级,但是无法找到任何标志或函数调用来实现这一点.我还查看了BaseHTTPRequestHandler类,但找不到线索.我相信一定有办法.如果你这样做,请与我和其他人分享; 我感谢您的努力.

python basehttpserver httpserver

55
推荐指数
1
解决办法
2万
查看次数

Python 3.x BaseHTTPServer或http.server

我正在尝试制作BaseHTTPServer程序.我更喜欢使用Python 3.3或3.2.我发现该文档很难理解导入的内容,但尝试更改导入:

from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
Run Code Online (Sandbox Code Playgroud)

至:

from http.server import BaseHTTPRequestHandler,HTTPServer
Run Code Online (Sandbox Code Playgroud)

然后导入工作和程序启动并等待GET请求.但是当请求到达时会引发异常:

File "C:\Python33\lib\socket.py", line 317, in write return self._sock.send(b)
TypeError: 'str' does not support the buffer interface
Run Code Online (Sandbox Code Playgroud)

问题:是否有一个版本的BaseHTTPServer或http.server与Python3.x开箱即用,或者我做错了什么?

这是我尝试在Python 3.3和3.2中运行的"我的"程序:

#!/usr/bin/python
# from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
from http.server import BaseHTTPRequestHandler,HTTPServer

PORT_NUMBER = 8080

# This class will handle any incoming request from
# a browser 
class myHandler(BaseHTTPRequestHandler):

    # Handler for the GET requests
    def do_GET(self):
        print   ('Get request received')
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        # Send the html message
        self.wfile.write("Hello World …
Run Code Online (Sandbox Code Playgroud)

python basehttpserver

48
推荐指数
3
解决办法
6万
查看次数

如何在BaseHTTPRequestHandler.do_POST()中提取HTTP消息体?

在我的do_POST()方法中,BaseHTTPRequestHandler我只需通过属性访问POST请求的标题self.headers.但是我找不到用于访问消息正文的类似属性.那我怎么去做呢?

python post http basehttpserver basehttprequesthandler

44
推荐指数
1
解决办法
3万
查看次数

如何在BaseHTTPRequestHandler子类中停止BaseHTTPServer.serve_forever()?

HTTPServer在一个单独的线程中运行我(使用无法停止线程的线程模块...)并希望在主线程也关闭时停止提供请求.

Python文档声明它BaseHTTPServer.HTTPServer是一个子类SocketServer.TCPServer,它支持一个shutdown方法,但它缺少HTTPServer.

整个BaseHTTPServer模块的文档很少:(

python http basehttpserver

42
推荐指数
7
解决办法
6万
查看次数

从BaseHTTPHandler解析http GET和POST参数?

来自BaseHTTPServer模块的BaseHTTPHandler似乎没有提供任何方便的方式来访问http请求参数.解析路径中的GET参数和请求体中的POST参数的最佳方法是什么?

现在,我正在使用它进行GET:

def do_GET(self):
    parsed_path = urlparse.urlparse(self.path)
    try:
        params = dict([p.split('=') for p in parsed_path[4].split('&')])
    except:
        params = {}
Run Code Online (Sandbox Code Playgroud)

这适用于大多数情况,但我想要一些更健壮的东西来正确处理编码和空参数等情况.理想情况下,我想要一些小而独立的东西,而不是一个完整的Web框架.

python http basehttpserver

35
推荐指数
3
解决办法
6万
查看次数

BaseHTTPServer和SimpleHTTPServer有什么区别?何时何地使用它?

BaseHTTPServer和SimpleHTTPServer有什么区别?我应该在何时何地使用这些?

python http basehttpserver simplehttpserver

16
推荐指数
1
解决办法
5638
查看次数

具有自定义实例的BaseHTTPRequestHandler

这是我的http服务器:

from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer

class test:
    def show(self):
        return "aaaa"

class http_server:
    def __init__(self, t1):
        self.t1 = t1
        server = HTTPServer(('', 8080), myHandler)
        server.serve_forever()

class myHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.end_headers()
        self.wfile.write(self.t1.show()) #Doesnt work
        return

class main:
    def __init__(self):
        self.t1 = test()
        self.server = http_server(self.t1)

if __name__ == '__main__':
    m = main()
Run Code Online (Sandbox Code Playgroud)

我需要访问myHander中的实例t1.

有什么方法可以做到吗?

谢谢!

python basehttpserver httpserver requesthandler basehttprequesthandler

14
推荐指数
3
解决办法
2万
查看次数

Python - BaseHTTPServer.HTTPServer并发和线程

有没有办法使BaseHTTPServer.HTTPServer像SocketServer.ThreadingTCPServer一样多线程?

python multithreading basehttpserver httpserver socketserver

13
推荐指数
1
解决办法
1万
查看次数

Python BaseHTTPServer,如何捕获/捕获"破管"错误?

我在Python中构建了一个简短的url转换器引擎,我看到了一个"破管"错误的TON,我很好奇如何在使用BaseHTTPServer类时最好地捕获它.这不是整个代码,但是让您了解到目前为止我在做什么:

    from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    import memcache

    class clientThread(BaseHTTPRequestHandler):

            def do_GET(self):
                    content = None
                    http_code,response_txt,long_url = \
                            self.ag_trans_url(self.path,content,'GET')
                    self.http_output( http_code, response_txt, long_url )
                    return

            def http_output(self,http_code,response_txt,long_url):
                    self.send_response(http_code)
                    self.send_header('Content-type','text/plain')
                    if long_url:
                            self.send_header('Location', long_url)
                    self.end_headers()
                    if response_txt:
                            self.wfile.write(response_txt)
                    return

        def ag_trans_url(self, orig_short_url, post_action, getpost):
                short_url = 'http://foo.co' + orig_short_url

                # fetch it from memcache
                long_url = mc.get(short_url)

                # other magic happens to look it up from db if there was nothing
                # in memcache, etc
                return (302, None, log_url)

def populate_memcache() …
Run Code Online (Sandbox Code Playgroud)

python exception-handling broken-pipe basehttpserver

13
推荐指数
3
解决办法
2万
查看次数

Python:BaseHTTPRequestHandler - 阅读原始帖子

如何阅读原始http帖子STRING.我找到了几个用于读取帖子的解析版本的解决方案,但是我正在处理的项目提交了一个没有标题的原始xml有效负载.所以我试图找到一种方法来读取post数据,而不将其解析为key => value数组.

python basehttpserver httpserver basehttprequesthandler

12
推荐指数
3
解决办法
1万
查看次数