1 |
ryzzyj |
1.1 |
import mimetools
|
2 |
|
|
import mimetypes
|
3 |
ryzzyj |
1.2 |
import urllib
|
4 |
ryzzyj |
1.1 |
import urllib2
|
5 |
|
|
import sys
|
6 |
ryzzyj |
1.2 |
from optparse import OptionParser
|
7 |
ryzzyj |
1.1 |
import os.path
|
8 |
ryzzyj |
1.2 |
import urlparse
|
9 |
ryzzyj |
1.1 |
|
10 |
|
|
|
11 |
|
|
uploadField = 'myFile'
|
12 |
|
|
filelist = 'files.txt'
|
13 |
|
|
url = 'http://popcon2vm:8081/upload'
|
14 |
ryzzyj |
1.2 |
file = ''
|
15 |
ryzzyj |
1.1 |
|
16 |
|
|
class RequestForm:
|
17 |
|
|
|
18 |
|
|
def __init__(self, field, file, newname):
|
19 |
|
|
self.boundary = mimetools.choose_boundary()
|
20 |
|
|
self.field = field
|
21 |
|
|
self.filename = file
|
22 |
|
|
self.newname = newname
|
23 |
|
|
|
24 |
|
|
def getBody(self):
|
25 |
|
|
return open(self.filename).read()
|
26 |
|
|
|
27 |
|
|
def getMimetype(self):
|
28 |
|
|
return mimetypes.guess_type(self.filename)[0] or "application/octet-stream"
|
29 |
|
|
|
30 |
|
|
def getContentType(self):
|
31 |
|
|
return 'multipart/form-data; boundary=%s' % self.boundary
|
32 |
|
|
|
33 |
|
|
def __str__(self):
|
34 |
|
|
return '\r\n'.join(['--' + self.boundary,
|
35 |
|
|
'Content-Disposition: file; name="%s"; filename="%s"' % (self.field, self.newname),
|
36 |
|
|
'Content-Type: %s' % self.getMimetype(),
|
37 |
|
|
'',
|
38 |
|
|
self.getBody(),
|
39 |
|
|
'--' + self.boundary + '--',
|
40 |
|
|
''])
|
41 |
|
|
|
42 |
|
|
|
43 |
ryzzyj |
1.2 |
def validateArgs():
|
44 |
|
|
if options.file != None and os.path.exists(options.file) and os.path.isfile(options.file):
|
45 |
|
|
global file
|
46 |
|
|
file = options.file
|
47 |
|
|
elif options.filelist != None and os.path.exists(options.filelist) and os.path.isfile(options.filelist):
|
48 |
|
|
global filelist
|
49 |
|
|
filelist = options.filelist
|
50 |
|
|
if options.url != None:
|
51 |
|
|
try:
|
52 |
|
|
#if url is bad this command throws error
|
53 |
|
|
urllib2.urlopen(options.url)
|
54 |
|
|
global url
|
55 |
|
|
url = options.url
|
56 |
|
|
except:
|
57 |
|
|
print 'Bad url.'
|
58 |
|
|
sys.exit()
|
59 |
|
|
|
60 |
|
|
if options.field != None:
|
61 |
|
|
global uploadField
|
62 |
|
|
uploadField = options.field
|
63 |
|
|
|
64 |
|
|
|
65 |
ryzzyj |
1.1 |
if __name__ == "__main__":
|
66 |
|
|
|
67 |
ryzzyj |
1.2 |
parser = OptionParser()
|
68 |
|
|
parser.add_option("-f", "--file", dest = "file", type = 'string', help = "uploads file to the server. If specified file list is not used.")
|
69 |
|
|
parser.add_option("-l", "--list", dest = "filelist", type = 'string', default = filelist, help = "specifies custom list of files. Default list (files.txt) is used if not specified.")
|
70 |
|
|
parser.add_option("-u", "--url", dest = "url", type = 'string', default = url, help = "web address of the script, that handles upload")
|
71 |
|
|
parser.add_option("-d", "--field", dest = "field", type = 'string', default = uploadField, help = "name of the upload field of the form.")
|
72 |
|
|
(options, args) = parser.parse_args()
|
73 |
|
|
validateArgs()
|
74 |
|
|
|
75 |
|
|
if file != '':
|
76 |
|
|
file = (file, )
|
77 |
|
|
elif os.path.exists(filelist) and os.path.isfile(filelist):
|
78 |
ryzzyj |
1.1 |
file = open(filelist)
|
79 |
|
|
else:
|
80 |
|
|
print "No list of files to upload found. Closing program."
|
81 |
|
|
sys.exit()
|
82 |
|
|
|
83 |
|
|
for path in file:
|
84 |
|
|
path = path.replace("\n", "")
|
85 |
ryzzyj |
1.2 |
base = os.path.basename(os.path.dirname(path))
|
86 |
|
|
name = os.path.basename(path)
|
87 |
|
|
if base != '' and name != '':
|
88 |
|
|
filename = "%s_%s" % (base, name)
|
89 |
|
|
elif name != '':
|
90 |
|
|
filename = name
|
91 |
|
|
else:
|
92 |
|
|
print 'Wrong file path (%s). Uploading next file.' % path
|
93 |
|
|
continue
|
94 |
ryzzyj |
1.1 |
if os.path.exists(path) and os.path.isfile(path):
|
95 |
|
|
form = RequestForm(uploadField, path, filename)
|
96 |
|
|
request = urllib2.Request(url)
|
97 |
ryzzyj |
1.2 |
if request != None:
|
98 |
|
|
body = str(form)
|
99 |
|
|
request.add_header('Content-type', form.getContentType())
|
100 |
|
|
request.add_data(body)
|
101 |
ryzzyj |
1.1 |
|