6

it's the first time I'm working (or trying) with Python. I would like to POST/GET and DELETE. POST and GET is working good but I got a big headache when I try to delete something that I have posted. Could you please give some enlightening? Thanks in advance!

import time
import BaseHTTPServer
import cgi


HOST_NAME = '0.0.0.0' 
PORT_NUMBER = 8000 

datastorage = []

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_HEAD(s):
        s.send_response(200)
        s.send_header("Content-type", "text/html")
        s.end_headers()

    def do_POST(s):
        s.send_response(200)
        s.wfile.write("Ok")
        contentLengthAsList = s.headers.getheaders('content-length')
        contentLengthAsInt = int(contentLengthAsList[0])

        x = cgi.parse_qs(s.rfile.read(contentLengthAsInt), keep_blank_values=1)
        datastorage.append(x['myHttpData'][0])

        print(datastorage)
        s.end_headers()

    def do_GET(s):
        """Respond to a GET request."""
        s.send_response(200)
        s.send_header("Content-type", "text/html")
        s.end_headers()
        s.wfile.write("<html><head><title>MyPage</title></head>")
        s.wfile.write("<body><p>The messages</p>")
        for x in range(len(datastorage)):
          s.wfile.write("<form action='/' method='DELETE'>"+datastorage[x]+"<input type='hidden'     name='index' value='x'/><input type='submit' value='Remove'/></form></br>")  
        s.wfile.write("</body></html>")

    def do_DELETE(s):
        global datastorage
        print 'That is working'
        contentLengthAsList = s.headers.getheaders('content-length')    
        contentLengthAsInt = int(contentLengthAsList[0])

        x = cgi.parse_qs(s.rfile.read(contentLengthAsInt), keep_blank_values=1)
        datastorage = datastorage.pop(x)

        s.send_response(301) #Moved Permanently
        s.send_header("location", "/")
        s.end_headers()



if __name__ == '__main__':
     server_class = BaseHTTPServer.HTTPServer
     httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
     print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
     try:
          httpd.serve_forever()
     except KeyboardInterrupt:
          pass
     httpd.server_close()
     print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER)`
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
Havengar
  • 69
  • 2
  • 7
  • 5
    Thanks for posting code, that helps up help you. But you need to be more specific than "headache." What specific errors are you seeing? – ron rothman Dec 02 '13 at 14:22
  • 1
    I can't delete. I don't get any error in my terminal, but like I said, I can't delete. I tried to write a log in the DELETE method and I don't get it back. I have no idea if my DELETE method is complete or not. Thanks for your attention! – Havengar Dec 02 '13 at 14:31
  • 1
    How do you send DELETE request to your server? – justhalf Dec 02 '13 at 15:33

1 Answers1

0

You are probably parsing the input in a wrong way:

    x = cgi.parse_qs(s.rfile.read(contentLengthAsInt), keep_blank_values=1)

I get this:

curl -X POST http://localhost:8000/ -H myHttpData:zumba -F jwf=jwf

-

127.0.0.1 - - [13/Sep/2017 19:31:13] "POST / HTTP/1.1" 200 -
{'--------------------------df32aa80f10efd75\r\nContent-Disposition: form-data': [''], ' name': ['"jwf"\r\n\r\njwf\r\n--------------------------df32aa80f10efd75--\r\n']}

which is a "modern" way of posting a form. Try a simpler format (application/x-www-form-urlencoded) or figure out how to parse a multipart/form-data with form-boundaries