~abone/ubuntu/natty/popularity-contest/fix-742017

« back to all changes in this revision

Viewing changes to popcon-upload.py

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2005-07-21 17:46:13 UTC
  • mfrom: (1.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050721174613-u3xtfc8ch68rwtwu
Tags: 1.30ubuntu1
* Resynchronise with Debian.
* debian/config: Default USEHTTP to true on upgrades.
* popcon-upload-ubuntu: Replace ubuntulinux.org with ubuntu.com.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
import httplib, mimetypes, sys
4
 
 
5
 
def post_multipart(host, uri, fields, files):
6
 
    content_type, body = encode_multipart_formdata(fields,files)
7
 
    h = httplib.HTTPConnection(host)
8
 
    h.putrequest('POST', uri)
9
 
    h.putheader('content-type', content_type)
10
 
    h.putheader('content-length', str(len(body)))
11
 
    h.endheaders()
12
 
    h.send(body)
13
 
    response = h.getresponse()
14
 
    return response.read()
15
 
 
16
 
def encode_multipart_formdata(fields, files):
17
 
    BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
18
 
    CRLF = '\r\n'
19
 
    L = []
20
 
    for (key, value) in fields:
21
 
        L.append('--' + BOUNDARY)
22
 
        L.append('Content-Disposition: form-data; name="%s"' % key)
23
 
        L.append('')
24
 
        L.append(value)
25
 
    for (key, filename, value) in files:
26
 
        L.append('--' + BOUNDARY)
27
 
        L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
28
 
        L.append('Content-Type: %s' % get_content_type(filename))
29
 
        L.append('')
30
 
        L.append(value)
31
 
    L.append('--' + BOUNDARY + '--')
32
 
    L.append('')
33
 
    body = CRLF.join(L)
34
 
    content_type= 'multipart/form-data; boundary=%s' % BOUNDARY
35
 
    return content_type, body
36
 
 
37
 
def get_content_type(filename):
38
 
    return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
39
 
 
40
 
upfile = 'popcon-data'
41
 
uploadname = 'popcondata'
42
 
val = sys.stdin.read()
43
 
 
44
 
data = ((uploadname,upfile,val),)
45
 
ret= post_multipart('popcon.ubuntulinux.org', '/popcon-submit.cgi', '' , data)
46
 
if not ret == 'Thanks!\n': 
47
 
    sys.stderr.write("%s\n" % ret)
48
 
    sys.exit(1)