File: ws-demo.py

package info (click to toggle)
python-feedvalidator 0~svn1022-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 652 kB
  • ctags: 2,452
  • sloc: python: 9,481; makefile: 27; sh: 8
file content (59 lines) | stat: -rwxr-xr-x 1,767 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python

# This is a simple demo of validation through the web service.


WS_HOST = 'www.feedvalidator.org'
WS_URI = '/check.cgi'

import urllib, httplib
from xml.dom import minidom
from sys import exit

# Fetch the feed to validate
rawData = open('../testcases/rss/may/image_height_recommended.xml').read()

# Specify the content type, including the charset if known
hdrs = {'Content-Type': 'application/xml'}

# Simply POST the feed contents to the validator URL
connection=httplib.HTTPConnection(WS_HOST, 80)
connection.request('POST', WS_URI, rawData, hdrs)
response=connection.getresponse()

# The response is a SOAP message, as XML (otherwise there's a problem
#  with the validator)
try:
  document=minidom.parseString(response.read())
except:
  print "Server error, unable to validate:",response.status,response.reason
  print "(Unable to parse response as XML.)"
  exit(20)

# If the status is OK, validation took place.
if response.status == 200:
  errors = document.getElementsByTagName("text")
  if not errors:
    print "The feed is valid!"
    exit(0)
  else:
    # Errors were found
    for node in errors:
      print "".join([child.data for child in node.childNodes])
    exit(5)
 

# If there was a problem on the server, show details
elif response.status >= 500:
  errors = document.getElementsByTagName("faultstring")
  for node in errors:
    print "".join([child.data for child in node.childNodes])
  traceback = document.getElementsByTagNameNS("http://www.python.org/doc/current/lib/module-traceback.html", "traceback")
  if traceback:
    print "".join([child.data for child in traceback[0].childNodes])
  exit(10)
 
# The unexpected happened...
else:
  print "Unexpected server response:",response.status,response.reason
  exit(20)