Add http server to X-Plane Python plugin

Add http server to X-Plane Python plugin

Postby macnfly » Sat Jun 08, 2013 6:34 pm

Hi,
I am new to this forum and must admit that Python is not my best friend.

However I am forced to deal with it since I need to make a plugin server to be used in X-Plane.

The thing is that X-Plane has support for Python scripts and can run using this code as a bare minimun.

Code: Select all
from XPLMDefs import *
from XPLMUtilities import *
from XPLMProcessing import *
from XPLMDataAccess import *

class PythonInterface:
   
    def XPluginStart(self):
        self.Name = "PTserver"
        self.Sig = "SandyBarbour.Python.Template"
        self.Desc = "A test script for the Python Interface."
       
        self.ser = SocketPlugin()
       
        return self.Name, self.Sig, self.Desc
   
    def XPluginStop(self):
        pass
   
    def XPluginEnable(self):
        return 1

    def XPluginDisable(self):
        pass

    def XPluginReceiveMessage(self, inFromWho, inMessage, inParam):
        pass
 


Where I am struggling is to add a simple http server for sending and receiving a text string when an incomming request is made.
There are lots of general Python example code to be found online and I am able to start and use a server if I go via a Mac Terminal session.
Here is an example http://fragments.turtlemeat.com/pythonwebserver.php

The big problem is that the server must be threaded and non-blocking so it does not mess up the call backs in the plugin.
The classes in Python confuses me big time because it works in a diferent way that I am use tu in ex. Lua scripting.

Can someone please help me in the right direction.

Laid out simple:
Start a http server that serves and takes request in a parallel thread to the X-Plane script, and able to interact via a text string ......

Re Peter
macnfly
 
Posts: 2
Joined: Sat Jun 08, 2013 3:28 pm

Re: Add http server to X-Plane Python plugin

Postby gratuitous_arp » Sun Jun 09, 2013 11:31 pm

What have you read so far about multithreading in Python? I'm not sure about Lua, but Python threading is relatively easy compared to C/C++. Try reading this and see if you can figure out how to make the web server you posted be launched in a separate thread (one of the first hits on google for "python threading example"): http://stackoverflow.com/questions/2846 ... or-dummies

If you get stuck post the code and someone can help.

If you are interested there is also this page on the Python wiki that explains many different options for concurrency, not all of them thread-based: http://wiki.python.org/moin/Concurrency/

What do you mean by "able to interact via a text string"?
gratuitous_arp
 
Posts: 6
Joined: Tue Apr 16, 2013 11:10 pm

Re: Add http server to X-Plane Python plugin

Postby macnfly » Tue Jun 11, 2013 4:49 pm

Thanks for the answer.
I tried out the suggestion but could not get a server to work threaded. It just hangs on connecting.
So I have kind of given up for now and taken another path.

Re Peter
macnfly
 
Posts: 2
Joined: Sat Jun 08, 2013 3:28 pm

Re: Add http server to X-Plane Python plugin

Postby codyf86 » Mon Jul 29, 2013 3:09 pm

You can do this to run a threaded TCP server. This is in python 3, but is pretty much the same as the python 2 syntax.

You can also use socketserver.HTTPServer instead of socketserver.TCPServer. Just depends on exactly what your doing.

Code: Select all
import socket
import threading
import socketserver

class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):

    def handle(self):
        current_thread = threading.current_thread()
        self.data = self.request.recv(8192)
        self.yourdata = 'your data here'
        self.request.send(self.yourdata.encode('utf-8')) #!If sending plain text, if not send it as bytes without .encode.

class ThreadedTCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
    pass

server = ThreadedTCPServer(('localhost', yourport), ThreadedTCPRequestHandler)
server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
server_thread.start()
codyf86
 
Posts: 3
Joined: Mon Jul 08, 2013 7:25 pm


Return to Networking

Who is online

Users browsing this forum: No registered users and 0 guests