title: Python 2 do_POST http.server multipart/formdata to Python 3
body: <p><strong>Problem</strong>: Trying to translate instructor's <strong>python 2</strong> code to <strong>python 3</strong><br>
  <strong>Specific Problem</strong>: Cannot access message field from the form in python 3</p>
  <h3><a href="https://github.com/lobrown/Full-Stack-Foundations/blob/master/Lesson-2/post-web-server/webserver.py" rel="nofollow noreferrer">Instructor's Code Snippet From Udacity Full-Stack Foundations Course</a></h3>
  <pre><code>def do_POST(self):
              try:
                  self.send_response(301)
                  self.send_header('Content-type', 'text/html')
                  self.end_headers()
                  ctype, pdict = cgi.parse_header(
                      self.headers.getheader('content-type'))
                  if ctype == 'multipart/form-data':
                      fields = cgi.parse_multipart(self.rfile, pdict)
                      messagecontent = fields.get('message')
                  output = &quot;&quot;
                  output += &quot;&lt;html&gt;&lt;body&gt;&quot;
                  output += &quot; &lt;h2&gt; Okay, how about this: &lt;/h2&gt;&quot;
                  output += &quot;&lt;h1&gt; %s &lt;/h1&gt;&quot; % messagecontent[0]
                  output += '''&lt;form method='POST' enctype='multipart/form-data' action='/hello'&gt;&lt;h2&gt;What would you like me to say?&lt;/h2&gt;&lt;input name=&quot;message&quot; type=&quot;text&quot; &gt;&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt; &lt;/form&gt;'''
                  output += &quot;&lt;/body&gt;&lt;/html&gt;&quot;
                  self.wfile.write(output)
                  print output
              except:
                  pass
  </code></pre>
  <p>After looking up documentation, github repositories, stackoverflow posts, and spending countless hours... I could not figure out how to pull the messages field in python 3 like <code>fields.get('message')</code>.</p>
  <h3>My attempt</h3>
  <pre><code>def do_POST(self):
              try:
                  length = int(self.headers['Content-Length'])
                  print(self.headers['Content-Type'])
                  self.send_response(301)
                  self.send_header('Content-type', 'text/html')
                  self.end_headers()
                  post_data = parse_qs(self.rfile.read(length).decode('utf-8'))
                  self.wfile.write(&quot;Lorem Ipsum&quot;.encode(&quot;utf-8&quot;))
                  ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
                  if ctype == 'multipart/form-data':
                      fields = cgi.parse_multipart(self.rfile, pdict)
                      messagecontent = fields.get('message')
      
                  output = ''
                  output += '&lt;html&gt;&lt;body&gt;'
                  output += '&lt;h2&gt; Okay, how about this: &lt;/h2&gt;'
                  output += '&lt;h1&gt; %s &lt;/h1&gt;' % messagecontent[0]
                  # You now have a dictionary of the post data
      
      
                  output += &quot;&lt;form method='POST' enctype='multipart/form-data' action='/hello'&gt;&lt;h2&gt;What would you like me to say?&lt;/h2&gt;&lt;input name='message' type='text'&gt;&lt;input type='submit' value='Submit'&gt;&lt;/form&gt;&quot;
                  output += '&lt;/html&gt;&lt;/body&gt;'
                  self.wfile.write(output.encode('utf-8'))
      
              except:
                  print('Error!')
  </code></pre>
  <p>My post_data variable is a dictionary but I cannot find a way to pull out the 'hi' message that I typed into the form. I am also not sure if this is the right way to go about pulling the data from the form.</p>
  <pre><code>&gt;&gt;&gt; post_data
  {' name': ['&quot;message&quot;\r\n\r\nhi\r\n------WebKitFormBoundarygm0MsepKJXVrBubX--\r\n']}
  </code></pre>