forked from stroem/spotify-deamon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_http.py
More file actions
70 lines (63 loc) · 2.37 KB
/
test_http.py
File metadata and controls
70 lines (63 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
import sys
sys.path.append("src/");
from configreader import *
############################
## HTTP POSTS
############################
import httplib
from xml.dom import minidom
def getText(nodelist):
rc = []
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc.append(node.data)
return ''.join(rc)
#def getconnection(self,url,handle_redirects=True,headers={}):
#"""Get a httplib connection for the given url.
#
# @param url: Should be a relative path for the HTTP request.
# @type url: string
# @param handle_redirects: Set this to True if you want to follow redirects (301 and 302). (default)
# @return: The httplib connection for the specified (or redirected) url."""
# if url.upper()[:6] == 'HTTPS:':
# conn = httplib.HTTPSConnection(self.host)
# else:
# conn = httplib.HTTPConnection(self.host)
# conn.connect()
# if not headers.has_key("User-Agent"):
# headers["User-Agent"] = self.agent
# if handle_redirects:
# # Now we handle 301 and 302 too!
# conn.request("HEAD", url,headers=headers)
# responseOb = conn.getresponse() ## Grab HTTPResponse Object
# if responseOb.status in (301,302,):
# url = urlparse.urljoin(url, responseOb.getheader('location', ''))
# conn = self.getconnection(url,True)
# return conn
def getdata(host, path):
print "Getting data for URL: http://" + host + "/" + path
output = ""
connection = httplib.HTTPConnection(host)
connection.request("GET", "/" + path)
response = connection.getresponse()
if response.status == 200:
output = response.read()
else:
if response.status == 302 and response.getheader("Location"):
output = getdata(host, response.getheader("Location").replace('http://','').replace(host + "/", ""))
else:
print "Error:", response.status, response.reason
print response.msg
print response.read()
connection.close()
return output
http_host = ConfigReader().get("app", "callback_host");
http_port = ConfigReader().get("app", "callback_port");
http_site = ConfigReader().get("app", "callback_site");
output = getdata(http_host + ":" + http_port, http_site)
if output:
dom = minidom.parseString(output)
elements = dom.getElementsByTagName("href")
if len(elements) > 0:
print getText(elements[0].childNodes)