source: QX/RPlain/http_import.py @ 957

Revision 957, 2.8 KB checked in by fhoffmann, 2 years ago (diff)
Line 
1from httplib import HTTP
2import cgi
3
4"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
5_globals.http_import:
6"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
7def http_import(url, method='GET', auth=None, parse_qs=0, timeout=5):
8
9  HTTP_PREFIX = 'http://'
10
11  # Get Query-String.
12  qs = ''
13  i = url.find('?')
14  if i > 0:
15    qs = url[i+1:]
16    url = url[:i]
17 
18  # Get Host.
19  host = ''
20  if len( host) == 0:
21 
22    # Remove HTTP-Prefix.
23    if url.startswith( HTTP_PREFIX):
24      url = url[ len( HTTP_PREFIX):]
25     
26    i = url.find('/')
27    if i > 0:
28      host = url[:i]
29      url = url[i:]
30    else:
31      host = url
32      url = '/'
33
34  # Get Port.
35  i = host.find(':',max(0,host.find('@')))
36  port = 80
37  if i > 0:
38    port = int(host[i+1:])
39    host = host[:i]
40 
41  # Open HTTP connection.
42  req = HTTP(host,port)
43 
44  # Set request-headers.
45  if method.upper() == 'GET':
46    if len( qs) > 0:
47      qs = '?' + qs
48    req.putrequest( method, url + qs)
49    req.putheader('Host', host)
50    authtobasic(auth,req)
51    req.putheader('Accept', '*/*')
52    req.endheaders()
53  elif method.upper() == 'POST':
54    req.putrequest(method,url)
55    req.putheader('Host', host)
56    authtobasic(auth,req)
57    req.putheader('Accept', '*/*')
58    req.putheader('Content-type', 'application/x-www-form-urlencoded')
59    req.putheader('Content-length', '%d' % len(qs))
60    req.endheaders()
61    # Send query string
62    req.send(qs)
63 
64  # Send request.
65  reply_code, message, headers = req.getreply()
66 
67  #### get parameter from content
68  if reply_code == 404 or reply_code >= 500:
69    error = "[%i]: %s at %s [%s]"%(reply_code,message,url,method)
70    raise error
71  elif reply_code==200:
72    # get content
73    f = req.getfile()
74    content = f.read()
75    f.close()
76    rtn = None
77    if parse_qs:
78      try:
79        # return dictionary of value lists
80        rtn = cgi.parse_qs(content, keep_blank_values=1, strict_parsing=1)
81      except:
82        # return string
83        rtn = content
84    else:
85      # return string
86      rtn = content
87      if port != 80:
88        rtn = rtn.replace( '%s%s/'%(HTTP_PREFIX,host), '%s%s:%i/'%(HTTP_PREFIX,host,port))
89    return rtn
90  else:
91    result = '['+str(reply_code)+']: '+str(message)
92    return result
93
94"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
95_globals.authtobasic:
96
97Basic Authentication
98"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
99def authtobasic(auth, h):
100 """Converts basic auth data into an HTTP header."""
101 import base64
102 if auth is not None:
103   userpass = auth['username']+':'+auth['password']
104   userpass = base64.encodestring(urllib.unquote(userpass)).strip()
105   h.putheader('Authorization', 'Basic '+userpass)
Note: See TracBrowser for help on using the repository browser.