2013-10-14, 06:57
(2013-10-14, 05:42)juggie Wrote:(2013-10-13, 21:59)Carb0 Wrote: juggie can you post the example code for a http server. It seems we need the http server solution to make live games work on Windows until apok fixed the bug in XBMC.
Hey Carb0,
I sent you a pm with a link to a python project I wrote that uses asyncore as a http server and also included info on how to run that within a python addon.
Let me know if that works for you, you would of course need to strip out the SSL aspect. If its not clear let me know and i'll whip up a short example I just do not have time right now.
Was able to get a quick example working prior to sleeping. There may be some unused vars in there as I stripped code from something else and did this quickly. You already know how to use urllib/httplib so inside handle_request you would parse the requested url and then request that url from neulion using proper agent string. Fix what you want to fix in the response, then call pushok() to return it to the other request.
Code:
#!/usr/bin/python
import asyncore, asynchat
import datetime
import socket, string, httplib, urllib2, urlparse
import StringIO, mimetools
import time
class HTTPChannel(asynchat.async_chat):
def __init__(self, server, sock, addr):
asynchat.async_chat.__init__(self, sock)
self.server = server
self.set_terminator("\r\n\r\n")
self.header = None
self.data = ""
self.shutdown = 0
def collect_incoming_data(self, data):
self.data = self.data + data
if len(self.data) > 16384:
# limit the header size to prevent attacks
self.shutdown = 1
def found_terminator(self):
if not self.header:
# parse http header
fp = StringIO.StringIO(self.data)
request = string.split(fp.readline(), None, 2)
if len(request) != 3:
# badly formed request; just shut down
self.shutdown = 1
else:
# parse message header
self.header = mimetools.Message(fp)
self.set_terminator("\r\n")
self.server.handle_request(
self, request[0], request[1], self.header
)
self.close_when_done()
self.data = ""
else:
pass # ignore body data, for now
def pushstatus(self, status, explanation="OK"):
self.push("HTTP/1.0 %d %s\r\n" % (status, explanation))
def pushok(self, content):
self.pushstatus(200, "OK")
self.push('Content-type: text/html\r\n')
self.push('Expires: Sat, 26 Jul 1997 05:00:00 GMT\r\n')
self.push('Last-Modified: '+ datetime.datetime.now().strftime("%d/%m/%Y %H:%M:%S")+' GMT\r\n')
self.push('Cache-Control: no-store, no-cache, must-revalidate\r\n' )
self.push('Cache-Control: post-check=0, pre-check=0\r\n')
self.push('Pragma: no-cache\r\n' )
self.push('\r\n')
self.push(content)
class AlarmServer(asyncore.dispatcher):
def __init__(self):
# Call parent class's __init__ method
asyncore.dispatcher.__init__(self)
# Create socket and listen on it
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.bind(("", 9999))
self.listen(5)
def handle_accept(self):
# Accept the connection
conn, addr = self.accept()
HTTPChannel(self, conn, addr)
def handle_request(self, channel, method, request, header):
query = urlparse.urlparse(request) # url is in here
query_array = urlparse.parse_qs(query.query, True) #any args are in here
if query.path == '/':
opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Demo')]
f = opener.open('http://www.whatsmyuseragent.com/')
channel.pushok(f.read())
else:
channel.pushok('requested something else')
if __name__=="__main__":
AlarmServer()
try:
while True:
asyncore.loop(timeout=2, count=1)
except KeyboardInterrupt:
print "Crtl+C pressed. Shutting down."