我正在尝试提供一个http服务器,该服务器响应来自客户端的POST请求。 python代码返回的是http代码000,而不是200/100。请让我知道代码中是否有任何问题
curl -s -o / dev / null -I -w“%{http_code}” -X POST https://10.1.4.179:443/xml -k 000
import BaseHTTPServer, SimpleHTTPServer
import ssl
import logging
class GetHandler (SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
logging.error(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
self.wfile.write("<html><body><h1>POST!</h1></body></html>")
Handler= GetHandler
logging.basicConfig(level=logging.DEBUG)
httpd=BaseHTTPServer.HTTPServer(("10.1.4.179", 443), Handler)
httpd.socket =ssl.wrap_socket(httpd.socket, certfile='/tftpboot/server.pem', server_side=True)
httpd.serve_forever()
答案 0 :(得分:0)
尝试在POST代码部分中发送响应代码:
from http.server import BaseHTTPRequestHandler,HTTPServer, SimpleHTTPRequestHandler
import ssl
import logging
class GetHandler(SimpleHTTPRequestHandler):
def do_GET(self):
logging.error(self.headers)
SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
self.data_string = self.rfile.read(int(self.headers['Content-Length']))
data = b'<html><body><h1>POST!</h1></body></html>'
self.wfile.write(bytes(data))
return
Handler=GetHandler
logging.basicConfig(level=logging.DEBUG)
httpd=HTTPServer(("localhost", 8080), Handler)
#httpd.socket =ssl.wrap_socket(httpd.socket, certfile='/tftpboot/server.pem', server_side=True)
httpd.serve_forever()
我打了一个curl -X POST http://localhost:8080/
,回复是
<html><body><h1>POST!</h1></body></html>
唯一的事情是我通过HTTP发出了请求,因此注释了加载证书的行。