~kiko/checkbox/pluggable-questions

« back to all changes in this revision

Viewing changes to hwtest/transport.py

  • Committer: Christian Reis
  • Date: 2007-09-19 21:45:48 UTC
  • Revision ID: christian.reis@canonical.com-20070919214548-g105a8uww595nl0y
Rip out curl support, and use urllib2 and a contributed module that handle file posts to submit data. Format data for submission to Launchpad. Almost works -- the hard part, posting the data, is sorted.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from cStringIO import StringIO
2
2
import time
3
3
import logging
 
4
import urllib2
4
5
 
5
6
import pycurl
6
7
 
7
8
from hwtest import API, VERSION
8
9
from hwtest.constants import MACHINE_ID_HEADER, MESSAGE_API_HEADER
9
10
from hwtest.log import format_delta
 
11
from hwtest.contrib import urllib2_file
10
12
 
11
13
 
12
14
class HTTPTransport(object):
19
21
    def get_url(self):
20
22
        return self._url
21
23
 
22
 
    def _post(self, payload, machine_id):
23
 
        curl = pycurl.Curl()
24
 
        curl.setopt(pycurl.URL, self._url)
25
 
        curl.setopt(pycurl.FOLLOWLOCATION, True)
26
 
        curl.setopt(pycurl.MAXREDIRS, 5)
27
 
 
28
 
        headers = ["%s: %s" % (MESSAGE_API_HEADER, API,),
29
 
                   "User-Agent: hwtest/%s" % (VERSION,),
30
 
                   "Content-Type: application/octet-stream"]
31
 
        if machine_id:
32
 
            headers.append("%s: %s" % (MACHINE_ID_HEADER, machine_id,))
33
 
 
34
 
        curl.setopt(pycurl.HTTPHEADER, headers)
35
 
        curl.setopt(pycurl.POST, True)
36
 
 
37
 
        # HACK: waiting for pubkey from IS team
38
 
        curl.setopt(pycurl.SSL_VERIFYHOST, 1)
39
 
        curl.setopt(pycurl.SSL_VERIFYPEER, False)
40
 
 
41
 
        if self._url.startswith("https") and self._pubkey is not None:
42
 
            curl.setopt(pycurl.CAINFO, self._pubkey)
43
 
 
44
 
        # HACK: curl can't get size if data has a \0
45
 
        curl.setopt(pycurl.POSTFIELDSIZE, len(payload))
46
 
        curl.setopt(pycurl.POSTFIELDS, payload)
47
 
 
48
 
        io = StringIO()
49
 
        curl.setopt(pycurl.WRITEFUNCTION, io.write)
50
 
 
51
 
        curl.perform()
52
 
        return curl, io.getvalue()
53
 
 
54
 
    def exchange(self, payload, machine_id=None):
 
24
    def _post(self, form):
 
25
        opener = urllib2.build_opener()
 
26
        opener.addheaders = [(MESSAGE_API_HEADER, API),
 
27
                             ("User-Agent", "hwtest/%s" % (VERSION,))]
 
28
        import pdb; pdb.set_trace()
 
29
        ret = opener.open(self._url, form)
 
30
        return ret
 
31
 
 
32
    def exchange(self, form):
55
33
        """Exchange message data with the server.
56
34
 
57
35
        THREAD SAFE (HOPEFULLY)
58
36
        """
59
37
        try:
60
38
            start_time = time.time()
61
 
            curly, data = self._post(payload, machine_id)
 
39
            curly, data = self._post(form)
62
40
            logging.info("Sent %d bytes and received %d bytes in %s.",
63
 
                         len(payload), len(data),
 
41
                         666, len(data),
64
42
                         format_delta(time.time() - start_time))
65
43
        except:
66
44
            logging.exception("Error contacting the server")