+ simplify porting of Web module to Python3
This commit is contained in:
@@ -24,27 +24,118 @@
|
||||
#include "PreCompiled.h"
|
||||
#ifndef _PreComp_
|
||||
# include <Python.h>
|
||||
# include <climits>
|
||||
# include <sstream>
|
||||
#endif
|
||||
|
||||
#include <Base/Console.h>
|
||||
|
||||
#include <CXX/Extensions.hxx>
|
||||
#include <CXX/Objects.hxx>
|
||||
#include "Server.h"
|
||||
|
||||
/* registration table */
|
||||
extern struct PyMethodDef Web_methods[];
|
||||
// See http://docs.python.org/2/library/socketserver.html
|
||||
/*
|
||||
import socket
|
||||
import threading
|
||||
import SocketServer
|
||||
|
||||
PyDoc_STRVAR(module_Web_doc,
|
||||
"This module is the Web module.");
|
||||
|
||||
ip = "127.0.0.1"
|
||||
port=54880
|
||||
|
||||
def client(ip, port, message):
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.connect((ip, port))
|
||||
try:
|
||||
sock.sendall(message)
|
||||
response = sock.recv(1024)
|
||||
print "Received: {}".format(response)
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
|
||||
|
||||
client(ip, port, "print 'Hello World 1'")
|
||||
client(ip, port, "import FreeCAD\nFreeCAD.newDocument()")
|
||||
client(ip, port, "Hello World 3\n")
|
||||
|
||||
*/
|
||||
|
||||
namespace Web {
|
||||
class Module : public Py::ExtensionModule<Module>
|
||||
{
|
||||
public:
|
||||
Module() : Py::ExtensionModule<Module>("Web")
|
||||
{
|
||||
add_varargs_method("startServer",&Module::startServer,
|
||||
"startServer(address=127.0.0.1,port=0) -- Start a server."
|
||||
);
|
||||
add_varargs_method("registerServerFirewall",&Module::registerServerFirewall,
|
||||
"registerServerFirewall(callable(string)) -- Register a firewall."
|
||||
);
|
||||
initialize("This module is the Web module."); // register with Python
|
||||
}
|
||||
|
||||
virtual ~Module() {}
|
||||
|
||||
private:
|
||||
Py::Object startServer(const Py::Tuple& args)
|
||||
{
|
||||
const char* addr = "127.0.0.1";
|
||||
int port=0;
|
||||
if (!PyArg_ParseTuple(args.ptr(), "|si",&addr,&port))
|
||||
throw Py::Exception();
|
||||
if (port > USHRT_MAX) {
|
||||
throw Py::OverflowError("port number is greater than maximum");
|
||||
}
|
||||
else if (port < 0) {
|
||||
throw Py::OverflowError("port number is lower than 0");
|
||||
}
|
||||
|
||||
AppServer* server = new AppServer();
|
||||
if (server->listen(QHostAddress(QString::fromLatin1(addr)), port)) {
|
||||
QString a = server->serverAddress().toString();
|
||||
quint16 p = server->serverPort();
|
||||
Py::Tuple t(2);
|
||||
t.setItem(0, Py::String((const char*)a.toLatin1()));
|
||||
t.setItem(1, Py::Int(p));
|
||||
return t;
|
||||
}
|
||||
else {
|
||||
server->deleteLater();
|
||||
std::stringstream out;
|
||||
out << "Server failed to listen at address " << addr << " and port " << port;
|
||||
throw Py::RuntimeError(out.str());
|
||||
}
|
||||
|
||||
return Py::None();
|
||||
}
|
||||
|
||||
Py::Object registerServerFirewall(const Py::Tuple& args)
|
||||
{
|
||||
PyObject* obj;
|
||||
if (!PyArg_ParseTuple(args.ptr(), "O",&obj))
|
||||
throw Py::Exception();
|
||||
|
||||
Py::Object pyobj(obj);
|
||||
if (pyobj.isNone())
|
||||
Web::Firewall::setInstance(0);
|
||||
else
|
||||
Web::Firewall::setInstance(new Web::FirewallPython(pyobj));
|
||||
|
||||
return Py::None();
|
||||
}
|
||||
};
|
||||
} // namespace Web
|
||||
|
||||
|
||||
/* Python entry */
|
||||
extern "C" {
|
||||
void WebAppExport initWeb() {
|
||||
PyMODINIT_FUNC initWeb() {
|
||||
|
||||
// ADD YOUR CODE HERE
|
||||
//
|
||||
//
|
||||
(void) Py_InitModule3("Web", Web_methods, module_Web_doc); /* mod name, table ptr */
|
||||
new Web::Module();
|
||||
Base::Console().Log("Loading Web module... done\n");
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
Reference in New Issue
Block a user