urllib2 Research - kstrack-grose/Git-Project GitHub Wiki

Megan and Carlos

Below is the structure of the code that we will need in order to upload "files" into a "website or storage system." We are unsure how this module can be use with PHP. How does Python communicate with PHP? We can also modify this code in order to download "files" from a "website or storage system." We need to meet with Jeffrey and Arthur!

import urllib2

import formdata

fields = {'name': 'BOB SMITH'} files = {'file': {'filename': 'F.DAT', 'content': 'DATA HERE'}} data, headers = formdata.encode_multipart(fields, files) request = urllib2.Request('http://httpbin.org/post', data=data, headers=headers) f = urllib2.urlopen(request) print f.read()

http://code.activestate.com/recipes/578668-encode-multipart-form-data-for-uploading-files-via/

from pprint import pformat import cgi

def application(environ, start_response): # show the environment: output = ['

']
output.append(pformat(environ))
output.append('
')
if environ['REQUEST_METHOD'] == 'POST':
    # show form data as received by POST:
form = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ)
    output.append('<h1>FORM DATA</h1>')
    output.append(form['test'].value)
    if form['test'].value == "dogs":
	output.append("<p>HAPPINESS")
else:
#create a simple form if no input has been posted to us
    output.append('<form method="post">')
    output.append('<input type="text" name="test">')
    output.append('<input type="submit">')
    output.append('</form>')



# send results
output_len = sum(len(line) for line in output)
start_response('200 OK', [('Content-type', 'text/html'),
                          ('Content-Length', str(output_len))])
return output
⚠️ **GitHub.com Fallback** ⚠️