Skip to content

Instantly share code, notes, and snippets.

@Olgagr
Last active June 5, 2017 09:41
Show Gist options
  • Save Olgagr/ece556d25281fa3fb16bfab067df2da1 to your computer and use it in GitHub Desktop.
Save Olgagr/ece556d25281fa3fb16bfab067df2da1 to your computer and use it in GitHub Desktop.
upload file
def _uploadFile(self):
form = MultiPartForm()
f = None
try:
f = io.open(self.dockwidget.filePathLineEdit.text(), 'r', encoding='utf-8') # opening file
form.add_file('test', 'test.txt', f)
request = urllib2.Request('http://localhost:8080/upload')
body = unicode(form)
request.add_header('Content-type', form.get_content_type())
request.add_header('Content-length', len(body))
request.add_data(body)
urllib2.urlopen(request) # here I get an error
# Traceback (most recent call last):
# File "/Users/ograbek/.qgis2/python/plugins/NetModeler/net_modeler.py", line 293, in _uploadFile urllib2.urlopen(request)
# File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen return _opener.open(url, data, timeout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment