-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.py
76 lines (62 loc) · 2.65 KB
/
upload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#Justin T's corner for PycURL tutorials and example functions.
import pycurl
import certifi
import urllib
import urlparse
from io import BytesIO #for getFromHttps()
import pycurl
try:
# python 3
from urllib.parse import urlencode
except ImportError:
# python 2
from urllib import urlencode
#code from http://pycurl.io/docs/latest/quickstart.html, I'm learning what it all does -Justin T.
#below: Post a given file to a given url using pycurl - url format should be 'https://website.com/' (including single quotes)
def _postToHttps(url, filename):
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.HTTPPOST, [
('fileupload', (
c.FORM_BUFFER, filename,
c.FORM_BUFFERPTR, 'insert description here',
)),
])
#{field : value} needs to be filled in. How?
post_data = {'field': 'value'}
# Form data must be provided already urlencoded. Function defined here: https://docs.python.org/2/library/urllib.html#urllib.urlencode
postfields = urlencode(post_data)
# Sets request method to POST,
# Content-Type header to application/x-www-form-urlencoded (the data type expected of string arguments)
# and data to send in request body.
c.setopt(c.POSTFIELDS, postfields)
c.perform()
c.close()
#below: Code from https://stackoverflow.com/questions/24572210/what-is-the-equivalent-to-curls-data-urlencode-in-pycurl
def _encodeData(url, queryString):
your_dict = {urlparse.parse_qs(queryString)}
res = urllib.request.urlopen(url), urllib.parse.urlencode(your_dict).encode()
# Get the result
return(res.read())
# urlparse.parse_qs() defined here: https://docs.python.org/2/library/urlparse.html#module-urlparse
# parse_qs() returns a dictionary made from the string argument (the query string)
# If we need the string as name & value data pairs we should use parse_qsl()
# Code from https://stackabuse.com/using-curl-in-python-with-pycurl/ - OPTION 1
# and http://pycurl.io/docs/latest/quickstart.html - OPTION 2
# below: pull from a given url, written in bytes - url format should be 'https://website.com/' (including single quotes)
def _getFromHttps(url, filename):
c = pycurl.Curl()
c.setopt(c.URL, url)
# OPTION 1: Write bytes into the given file. File format should be 'file.txt' (including single quotes)
c.setopt(c.WRITEDATA, filename)
c.perform()
c.close()
def _getFromHttps(url):
c = pycurl.Curl()
c.setopt(c.URL, url)
# OPTION 2: Write bytes to a BytesIO object. Not sure how this is stored.
buffer = BytesIO()
c.setopt(c.WRITEFUNCTION, buffer.write)
c.setopt(c.CAINFO, certifi.where()) #HTTPS Certificate
c.perform()
c.close()