Guest User

XBMCLocalProxy

a guest
Feb 26th, 2011
1,530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. """
  2. XBMCLocalProxy 0.1
  3. Copyright 2011 Torben Gerkensmeyer
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. MA 02110-1301, USA.
  19. """
  20.  
  21. import base64
  22. import re
  23. import time
  24. import urllib
  25. import sys
  26. import traceback
  27. import socket
  28. from SocketServer import ThreadingMixIn
  29. from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
  30. from urllib import *
  31.  
  32. class MyHandler(BaseHTTPRequestHandler):
  33. """
  34. Serves a HEAD request
  35. """
  36. def do_HEAD(s):
  37. print "XBMCLocalProxy: Serving HEAD request..."
  38. s.answer_request(0)
  39.  
  40. """
  41. Serves a GET request.
  42. """
  43. def do_GET(s):
  44. print "XBMCLocalProxy: Serving GET request..."
  45. s.answer_request(1)
  46.  
  47. def answer_request(s, sendData):
  48. try:
  49. request_path=s.path[1:]
  50. request_path=re.sub(r"\?.*","",request_path)
  51. if request_path=="stop":
  52. sys.exit()
  53. elif request_path=="version":
  54. s.send_response(200)
  55. s.end_headers()
  56. s.wfile.write("Proxy: Running\r\n")
  57. s.wfile.write("Version: 0.1")
  58. elif request_path[0:12]=="withheaders/":
  59. (realpath,sep,additionalheaders)=request_path[12:].partition("/")
  60. fURL=base64.b64decode(realpath)
  61. additionalhString=base64.b64decode(additionalheaders)
  62. s.serveFile(fURL, additionalhString, sendData)
  63. else:
  64. s.send_response(403)
  65. except:
  66. traceback.print_exc()
  67. s.wfile.close()
  68. return
  69. try:
  70. s.wfile.close()
  71. except:
  72. pass
  73.  
  74.  
  75. """
  76. Sends the requested file and add additional headers.
  77. """
  78. def serveFile(s, fURL, additionalhstring, sendData):
  79. additionalh=s.decodeHeaderString(additionalhstring)
  80. opener = FancyURLopener()
  81. opener.addheaders=[]
  82. d={}
  83. sheaders=s.decodeHeaderString("".join(s.headers.headers))
  84. for key in sheaders:
  85. d[key]=sheaders[key]
  86. if (key!="Host"): opener.addheader(key,sheaders[key])
  87. for key in additionalh:
  88. d[key]=additionalh[key]
  89. opener.addheader(key,additionalh[key])
  90. response = opener.open(fURL)
  91. s.send_response(response.code)
  92. print "XBMCLocalProxy: Sending headers..."
  93. headers=response.info()
  94. for key in headers:
  95. try:
  96. val=headers[key]
  97. s.send_header(key, val)
  98. except Exception, e:
  99. print e
  100. pass
  101. s.end_headers()
  102.  
  103. if (sendData):
  104. print "XBMCLocalProxy: Sending data..."
  105. fileout=s.wfile
  106. try:
  107. buf="INIT"
  108. try:
  109. while (buf!=None and len(buf)>0):
  110. buf=response.read(8*1024)
  111. fileout.write(buf)
  112. fileout.flush()
  113. response.close()
  114. fileout.close()
  115. print time.asctime(),"Closing connection"
  116. except socket.error, e:
  117. print time.asctime(),"Client Closed the connection."
  118. try:
  119. response.close()
  120. fileout.close()
  121. except Exception, e:
  122. return
  123. except Exception,e:
  124. traceback.print_exc(file=sys.stdout)
  125. response.close()
  126. fileout.close()
  127. except:
  128. traceback.print_exc()
  129. s.wfile.close()
  130. return
  131. try:
  132. s.wfile.close()
  133. except:
  134. pass
  135.  
  136. def decodeHeaderString(self,hs):
  137. di={}
  138. hss=hs.replace("\r","").split("\n")
  139. for line in hss:
  140. u=line.split(": ")
  141. try:
  142. di[u[0]]=u[1]
  143. except:
  144. pass
  145. return di
  146.  
  147.  
  148. class Server(HTTPServer):
  149. """HTTPServer class with timeout."""
  150.  
  151. def get_request(self):
  152. """Get the request and client address from the socket."""
  153. self.socket.settimeout(5.0)
  154. result = None
  155. while result is None:
  156. try:
  157. result = self.socket.accept()
  158. except socket.timeout:
  159. pass
  160. result[0].settimeout(1000)
  161. return result
  162.  
  163. class ThreadedHTTPServer(ThreadingMixIn, Server):
  164. """Handle requests in a separate thread."""
  165.  
  166. HOST_NAME = '127.0.0.1'
  167. PORT_NUMBER = 64653
  168.  
  169. if __name__ == '__main__':
  170. socket.setdefaulttimeout(10)
  171. server_class = ThreadedHTTPServer
  172. httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
  173. print "XBMCLocalProxy Starts - %s:%s" % (HOST_NAME, PORT_NUMBER)
  174. while(True):
  175. httpd.handle_request()
  176. httpd.server_close()
  177. print "XBMCLocalProxy Stops %s:%s" % (HOST_NAME, PORT_NUMBER)
Add Comment
Please, Sign In to add comment