~barry/ubuntu/oneiric/pycurl/bug-788514

« back to all changes in this revision

Viewing changes to tests/test_multi_socket.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-05-24 13:00:47 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070524130047-f1eds51xycdubvpt
Tags: 7.16.2.1-2ubuntu1
* Merge from Debian; remaining changes:
  - Build a python-pycurl-dbg package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
# -*- coding: iso-8859-1 -*-
 
3
# vi:ts=4:et
 
4
# $Id: test_multi_socket.py,v 1.1 2006/11/10 15:03:05 kjetilja Exp $
 
5
 
 
6
import os, sys
 
7
try:
 
8
    from cStringIO import StringIO
 
9
except ImportError:
 
10
    from StringIO import StringIO
 
11
import pycurl
 
12
 
 
13
 
 
14
urls = (
 
15
    "http://curl.haxx.se",
 
16
    "http://www.python.org",
 
17
    "http://pycurl.sourceforge.net",
 
18
)
 
19
 
 
20
# Read list of URIs from file specified on commandline
 
21
try:
 
22
    urls = open(sys.argv[1], "rb").readlines()
 
23
except IndexError:
 
24
    # No file was specified
 
25
    pass
 
26
 
 
27
# timer callback
 
28
def timer(msecs):
 
29
    print 'Timer callback msecs:', msecs
 
30
 
 
31
# socket callback
 
32
def socket(event, socket, multi, data):
 
33
    print event, socket, multi, data
 
34
#    multi.assign(socket, timer)
 
35
 
 
36
# init
 
37
m = pycurl.CurlMulti()
 
38
m.setopt(pycurl.M_PIPELINING, 1)
 
39
m.setopt(pycurl.M_TIMERFUNCTION, timer)
 
40
m.setopt(pycurl.M_SOCKETFUNCTION, socket)
 
41
m.handles = []
 
42
for url in urls:
 
43
    c = pycurl.Curl()
 
44
    # save info in standard Python attributes
 
45
    c.url = url
 
46
    c.body = StringIO()
 
47
    c.http_code = -1
 
48
    m.handles.append(c)
 
49
    # pycurl API calls
 
50
    c.setopt(c.URL, c.url)
 
51
    c.setopt(c.WRITEFUNCTION, c.body.write)
 
52
    m.add_handle(c)
 
53
 
 
54
# get data
 
55
num_handles = len(m.handles)
 
56
while num_handles:
 
57
     while 1:
 
58
         ret, num_handles = m.socket_all()
 
59
         if ret != pycurl.E_CALL_MULTI_PERFORM:
 
60
             break
 
61
     # currently no more I/O is pending, could do something in the meantime
 
62
     # (display a progress bar, etc.)
 
63
     m.select(1.0)
 
64
 
 
65
# close handles
 
66
for c in m.handles:
 
67
    # save info in standard Python attributes
 
68
    c.http_code = c.getinfo(c.HTTP_CODE)
 
69
    # pycurl API calls
 
70
    m.remove_handle(c)
 
71
    c.close()
 
72
m.close()
 
73
 
 
74
# print result
 
75
for c in m.handles:
 
76
    data = c.body.getvalue()
 
77
    if 0:
 
78
        print "**********", c.url, "**********"
 
79
        print data
 
80
    else:
 
81
        print "%-53s http_code %3d, %6d bytes" % (c.url, c.http_code, len(data))
 
82