1

I am trying to comeup with a http server which replies for a POST request from the client. The python code is returning http code 000 instead of 200/100. Plz let me know if there is any issue in the code

CURL request returns 000 instead of 200/100

curl -s -o /dev/null -I -w "%{http_code}" -X POST https://10.1.4.179:443/xml -k 000

CODE:

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()
piy26
1,61213 silver badges22 bronze badges
asked Jun 21, 2018 at 1:13

1 Answer 1

2

Try sending the reponse code in your POST code section:

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()

I made a call to curl -X POST http://localhost:8080/ and the response is

<html><body><h1>POST!</h1></body></html>

Only thing is I made the request over HTTP and hence commented out the line that load the certs.

answered Jun 21, 2018 at 1:29
Sign up to request clarification or add additional context in comments.

4 Comments

I have tried doing send_response(200) it does not help. Also same result without xml. Just to be clear, I do get the response xml reply "<html><body><h1>POST!</h1></body></html>" . But the problem is status is always 000. You could cut and paste and try urself.
PLZ let me know if somebody could help here. @ply26 did u get the choose to try your answers? did it work for you?
@dd76 Please check it now. I have updated the code and its working perfectly for me.
HI Piy26, Thank you so much. First thing its not about the o/p. Even the initial code provides me the required o/p. But the http status it returns was 0, instead of 200. That is my problem. If you could try the below curl, "curl -s -o /dev/null -I -w "%{http_code}" -X POST 10.1.4.179:443/xml -k". Also I am looking for server supporting SSL connection and not pure HTTP

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.