Posted By

atr000 on 05/31/09


Tagged

http post python handler


Versions (?)

python: handler for http post


 / Published in: Python
 

  1. Python Handlers
  2.  
  3. This is the python script that handles requests to the endpoints defined above. This file is located at $SPLUNK_HOME/etc/apps/WebSkunk/rest/webskunkhandlers.py.
  4.  
  5. #
  6. # http request handlers
  7. #
  8.  
  9. from splunk import auth, search
  10. import splunk.rest
  11. import utils
  12. import logging as logger
  13. import splunk.bundle as bundle
  14. import httplib2, urllib, os, time
  15. import telnetlib
  16.  
  17. # set our path to this particular application directory (which is suppose to be <appname>/bin)
  18. app_dir = os.path.dirname(os.path.abspath(__file__))
  19.  
  20. # define the web content directory (needs to be <appname>/web directory)
  21. web_dir = app_dir + "/web"
  22.  
  23. class main(splunk.rest.BaseRestHandler):
  24. '''
  25. Main endpoint
  26. '''
  27.  
  28. def handle_GET(self):
  29.  
  30. # set output params
  31. self.response.setStatus(200)
  32. self.response.setHeader('content-type', 'text/html')
  33. self.response.write('Main: this is webskunk')
  34.  
  35. # listen to all verbs
  36. handle_POST = handle_DELETE = handle_PUT = handle_VIEW = handle_GET
  37.  
  38. class status(splunk.rest.BaseRestHandler):
  39. '''
  40. Status endpoint
  41. '''
  42.  
  43. def handle_GET(self):
  44.  
  45. # set output params
  46. self.response.setStatus(200)
  47. self.response.setHeader('content-type', 'text/html')
  48. self.response.write('<html><head><title>webskunk</title></head><body>')
  49. self.response.write('<h1>Status: webskunk is online</h1><br><br>')
  50. self.response.write('<b>Active endpoints:</b><br>')
  51. self.response.write(utils.getHrefs(self.pathParts[1]))
  52. self.response.write('</body></html>')
  53.  
  54.  
  55. # listen to all verbs
  56. handle_POST = handle_DELETE = handle_PUT = handle_VIEW = handle_GET
  57.  
  58.  
  59. class web(splunk.rest.BaseRestHandler):
  60. '''
  61. Main endpoint
  62. '''
  63.  
  64. def handle_GET(self):
  65. files = []
  66.  
  67. logger.debug('scanning %s' % web_dir)
  68.  
  69. # do a scan of the application directory's web directory
  70. for root, dirs, file_list in os.walk(web_dir):
  71. cur_dir = root.replace(web_dir, '')
  72. for file in file_list:
  73. full_name = os.path.join(cur_dir, file)
  74. files.append(full_name)
  75.  
  76. # extract the last segment of the URL passed to splunkd and use it to load a file
  77. last_segment = self.pathParts[len(self.pathParts)-1]
  78.  
  79. # check the requested file exists in the web directory - if not, we return a 404
  80. if last_segment in files:
  81. f = open(web_dir + '/' + last_segment, 'rb')
  82. content = f.read()
  83. f.close()
  84.  
  85. # basic checking of file extension to set a mime type
  86. if ".html" in last_segment:
  87. self.response.setHeader('content-type', 'text/html')
  88. elif ".js" in last_segment:
  89. self.response.setHeader('content-type', 'application/x-javascript')
  90. elif ".css" in last_segment:
  91. self.response.setHeader('content-type', 'text/css')
  92. elif ".jpg" in last_segment:
  93. # this won't work for now - leaving here for a reminder
  94. self.response.setHeader('content-type', 'image/jpeg')
  95. content_length = "%d" % len(content)
  96. self.response.setHeader('Content-Length', content_length)
  97. self.response.setStatus(200)
  98. else:
  99. self.response.setStatus(404)
  100. content = "404 - not found!"
  101.  
  102. self.response.write(content)
  103.  
  104.  
  105. class receiver(splunk.rest.BaseRestHandler):
  106.  
  107. def handle_POST(self):
  108. host="localhost"
  109. tn = telnetlib.Telnet(host, 9998)
  110. try:
  111. content = self.args['splunkdata']
  112. tn.write(content)
  113. self.response.write("AWESOME!")
  114. except Exception, e:
  115. logger.exception(e)
  116. self.response.write(e)
  117.  
  118. # listen to all verbs
  119. handle_GET = handle_DELETE = handle_PUT = handle_VIEW = handle_POST

Report this snippet  

You need to login to post a comment.