gs.form API Reference

The package exports the following API symbol.

gs.form.post_multipart(netloc, selector, fields, files=, []usessl=False)

Post fields and files to an http host as multipart/form-data.

Parameters:
  • netloc (str) – The netloc (host or host:port).
  • selector (str) – The path to the form that will be posted to.
  • fields (list) – A sequence of (name, value) 2-tuple elements for regular form fields.
  • files (list) – A sequence of (name, filename, value) 3-tuple elements for data to be uploaded as files
  • usessl (bool) – True if TLS should be used to communicate with the server.
Returns:

A 3-tuple: the reponse-status, reason, and data.

Example:

Post three normal form fields (parrot, piranah, and ethyl) and one file (the text file rule.txt, sent as the unwritten form field) to example.com on port 2585, using normal HTTP rather than TLS (the default):

fields = [('parrot', 'dead'), ('piranha', 'brother'),
          ('ethyl', 'frog')]
files = [('unwritten', 'rule.txt', 'This is a transgression.')]
r = post_multipart('example.com:2585', '/form.html', fields, files)
status, reason, data = r

Internals

In addition to the post_multipart() function, the following are used internally.

class gs.form.postmultipart.Connection(netloc, usessl=False)[source]

A wrapper for the HTTP(S) connection

getresponse()[source]

Make a response. Wraps http.client.HTTPConnection.getresponse().

request(requestType, selector, body, headers)[source]

Make a request. Wraps http.client.HTTPConnection.request().

gs.form.postmultipart.encode_multipart_formdata(fields, files)[source]

Encode the data into a multipart-document

Parameters:
  • fields (list) – A sequence of (name, value) 2-tuple elements, for regular form fields.
  • files (list) – A sequence of (name, filename, value) 3-tuple elements for data to be uploaded as files
Returns:

(content_type, body) as a 2-tuple ready to be sent in a POST.

Return type:

tuple

gs.form.postmultipart.get_content_type(filename)[source]

Get the content type of a file

Parameters:filename (str) – The name of the file.
Returns:The MIME-type of the file, or application/octet-stream.
Return type:str