本文整理汇总了Python中BaseHTTPServer.BaseHTTPRequestHandler方法的典型用法代码示例。如果您正苦于以下问题:Python BaseHTTPServer.BaseHTTPRequestHandler方法的具体用法?Python BaseHTTPServer.BaseHTTPRequestHandler怎么用?Python BaseHTTPServer.BaseHTTPRequestHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BaseHTTPServer
的用法示例。
在下文中一共展示了BaseHTTPServer.BaseHTTPRequestHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def __init__(self,
request,
client_address,
connection_handler=httplib.HTTPConnection):
"""Constructor extending BaseHTTPRequestHandler.
Args:
request: The incoming request.
client_address: A (ip, port) tuple with the address of the client.
backend: The HTTPServer that received the request.
connection_handler: http library to use when balancer the connection to
the next available backend instance. Used for dependency injection.
"""
self.connection_handler = connection_handler
BaseHTTPServer.BaseHTTPRequestHandler.__init__(self,
request,
client_address,
HttpServer())
示例2: handle_get
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def handle_get(self):
"""Handle a single HTTP GET request.
Default implementation indicates an error because
XML-RPC uses the POST method.
"""
code = 400
message, explain = \
BaseHTTPServer.BaseHTTPRequestHandler.responses[code]
response = BaseHTTPServer.DEFAULT_ERROR_MESSAGE % \
{
'code' : code,
'message' : message,
'explain' : explain
}
print 'Status: %d %s' % (code, message)
print 'Content-Type: %s' % BaseHTTPServer.DEFAULT_ERROR_CONTENT_TYPE
print 'Content-Length: %d' % len(response)
print
sys.stdout.write(response)
示例3: __init__
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def __init__(self):
super(HttpServerThread, self).__init__()
self.done = False
self.hostname = 'localhost'
class MockStardog(BaseHTTPRequestHandler):
def do_GET(self):
if self.path != '/admin/status':
self.send_response(404)
return
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
# json.dumps always outputs a str, wfile.write requires bytes
self.wfile.write(ensure_bytes(json.dumps(DATA)))
self.http = HTTPServer((self.hostname, 0), MockStardog)
self.port = self.http.server_port
示例4: handle_get
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def handle_get(self):
"""Handle a single HTTP GET request.
Default implementation indicates an error because
XML-RPC uses the POST method.
"""
code = 400
message, explain = \
BaseHTTPServer.BaseHTTPRequestHandler.responses[code]
response = BaseHTTPServer.DEFAULT_ERROR_MESSAGE % \
{
'code' : code,
'message' : message,
'explain' : explain
}
print 'Status: %d %s' % (code, message)
print 'Content-Type: text/html'
print 'Content-Length: %d' % len(response)
print
sys.stdout.write(response)
示例5: start_server
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def start_server():
server_address = ('127.0.0.1', 0)
class DaemonThreadingMixIn(ThreadingMixIn):
daemon_threads = True
class ThreadedHTTPServer(DaemonThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
# not actually going to do anything with this server, so a
# do-nothing handler is reasonable
httpd = ThreadedHTTPServer(server_address, BaseHTTPRequestHandler)
server_thread = threading.Thread(target=httpd.serve_forever)
server_thread.daemon = True
server_thread.start()
return httpd, server_thread
示例6: setup
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def setup(self):
if isinstance(self.__class__.first_run, collections.Callable):
try:
with self.__class__.first_run_lock:
if isinstance(self.__class__.first_run, collections.Callable):
self.first_run()
self.__class__.first_run = None
except StandardError as e:
logging.exception('%s.first_run() return %r', self.__class__, e)
self.__class__.setup = BaseHTTPServer.BaseHTTPRequestHandler.setup
self.__class__.do_CONNECT = self.__class__.do_METHOD
self.__class__.do_GET = self.__class__.do_METHOD
self.__class__.do_PUT = self.__class__.do_METHOD
self.__class__.do_POST = self.__class__.do_METHOD
self.__class__.do_HEAD = self.__class__.do_METHOD
self.__class__.do_DELETE = self.__class__.do_METHOD
self.__class__.do_OPTIONS = self.__class__.do_METHOD
self.__class__.do_PATCH = self.__class__.do_METHOD
self.setup()
示例7: handle_one_request
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def handle_one_request(self):
if not self.disable_transport_ssl and self.scheme == 'http':
leadbyte = self.connection.recv(1, socket.MSG_PEEK)
if leadbyte in ('\x80', '\x16'):
server_name = ''
if leadbyte == '\x16':
for _ in xrange(2):
leaddata = self.connection.recv(1024, socket.MSG_PEEK)
if is_clienthello(leaddata):
try:
server_name = extract_sni_name(leaddata)
finally:
break
try:
certfile = CertUtil.get_cert(server_name or 'www.google.com')
ssl_sock = ssl.wrap_socket(self.connection, ssl_version=self.ssl_version, keyfile=certfile, certfile=certfile, server_side=True)
except StandardError as e:
if e.args[0] not in (errno.ECONNABORTED, errno.ECONNRESET):
logging.exception('ssl.wrap_socket(self.connection=%r) failed: %s', self.connection, e)
return
self.connection = ssl_sock
self.rfile = self.connection.makefile('rb', self.bufsize)
self.wfile = self.connection.makefile('wb', 0)
self.scheme = 'https'
return BaseHTTPServer.BaseHTTPRequestHandler.handle_one_request(self)
示例8: handle
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def handle(self):
''' To quiet socket errors with 404.
'''
try:
BaseHTTPServer.BaseHTTPRequestHandler.handle(self)
except Exception as error:
pass
示例9: __init__
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def __init__(self, *args):
try:
BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, *args)
except IOError:
pass
示例10: log_request
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def log_request(self, code='-', size='-'):
"""Indicate that this request has completed."""
BaseHTTPServer.BaseHTTPRequestHandler.log_request(self, code, size)
if code == '-':
code = 0
if size == '-':
size = 0
logservice.logs_buffer().flush()
self._log_record_writer.end_request(None, code, size)
示例11: handle_one_request
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def handle_one_request(self):
"""Override. Invoked from BaseHTTPRequestHandler constructor."""
self.raw_requestline = self.rfile.readline()
if not self.raw_requestline:
self.close_connection = 1
return
if not self.parse_request():
return
process = GlobalProcess()
balance_set = process.GetBalanceSet()
request_size = int(self.headers.get('content-length', 0))
payload = self.rfile.read(request_size)
for port in balance_set:
logging.debug('balancer to port %d', port)
connection = self.connection_handler(process.host, port=port)
connection.response_class = ForwardResponse
connection.request(self.command, self.path, payload, dict(self.headers))
try:
response = connection.getresponse()
except httplib.HTTPException, e:
self.send_error(httplib.INTERNAL_SERVER_ERROR, str(e))
return
if response.status != httplib.SERVICE_UNAVAILABLE:
self.wfile.write(response.data)
return
示例12: log_request
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def log_request(self, code='-', size='-'):
"""Selectively log an accepted request."""
if self.server.logRequests:
BaseHTTPServer.BaseHTTPRequestHandler.log_request(self, code, size)
示例13: serve
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def serve(port, callback=None, completer=None):
import BaseHTTPServer, mimetools, select
# Patch up mimetools.Message so it doesn't break if rfc822 is reloaded.
class Message(mimetools.Message):
def __init__(self, fp, seekable=1):
Message = self.__class__
Message.__bases__[0].__bases__[0].__init__(self, fp, seekable)
self.encodingheader = self.getheader('content-transfer-encoding')
self.typeheader = self.getheader('content-type')
self.parsetype()
self.parseplist()
class DocHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def send_document(self, title, contents):
try:
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write(html.page(title, contents))
except IOError: pass
def do_GET(self):
path = self.path
if path[-5:] == '.html': path = path[:-5]
if path[:1] == '/': path = path[1:]
if path and path != '.':
try:
obj = locate(path, forceload=1)
except ErrorDuringImport, value:
self.send_document(path, html.escape(str(value)))
return
if obj:
self.send_document(describe(obj), html.document(obj, path))
else:
self.send_document(path,
'no Python documentation found for %s' % repr(path))
else:
示例14: request_handler
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def request_handler(addr):
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/provider.json':
body = provider_json % {
'api_uri': addr.api_uri,
'host': addr.host,
'port': addr.port,
'fingerprint': addr.fingerprint
}
elif self.path == '/ca.crt':
cacert = os.path.join(os.path.dirname(__file__),
"leaptestscert.pem")
with open(cacert, 'r') as f:
castr = f.read()
body = castr
elif self.path == '/1/configs.json':
body = configs_json
else:
body = '{"error": "not implemented"}'
self.send_response(200)
self.send_header('Content-type', 'applicatino/json')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
self.wfile.write(body)
return RequestHandler
示例15: __init__
# 需要导入模块: import BaseHTTPServer [as 别名]
# 或者: from BaseHTTPServer import BaseHTTPRequestHandler [as 别名]
def __init__(self, *args, **kwargs):
BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, *args, **kwargs)