Qt HTTP 服务器?

发布于 2024-09-07 11:29:36 字数 1539 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

折戟 2024-09-14 11:29:36

我发现了这个 https://github.com/vinipsmaker/tufao
这是一个迟到的答案..不确定是否有帮助。

I found this https://github.com/vinipsmaker/tufao ,
Its a late answer .. not sure if it helps.

甜尕妞 2024-09-14 11:29:36

QtWebApp 是一个 HTTP 服务器,支持 GET 和 POST 方法、cookie、会话和文件上传。使用这个库就像编写 Java Servlet 一样简单。

该项目网站是德语的,但可下载的文件都是英文的,包括文档。

QtWebApp is a HTTP server with support of GET and POST method, cookies, sessions and file uploads. Using this library is as simple as writing a Java Servlet.

The project website is in german, however the downloadable files are all in english, including the documentation.

小姐丶请自重 2024-09-14 11:29:36

我刚刚发布了 QHttpEngine 的第一个版本,旨在填补您所描述的空白。该项目的目标是提供一组极其简单的类,这些类提供与 Qt 集成的 HTTP 服务器。

例如,要从应用程序中的资源提供静态文件,您需要做的就是:

QFilesystemHandler handler(":/www");
QHttpServer server(&handler);

server.listen(QHostAddress::LocalHost, 8000);

您还可以创建一个从 QObject 派生的类,将其插槽公开为 HTTP API 中的端点。例如:

class ApiHandler : public QObjectHandler
{
    Q_OBJECT

private slots:

    QVariantMap doSomething(const QVariantMap ¶ms) {
        // do something with the parameters and return a response
    }
};

然后,用户可以将参数编码为 JSON 的 POST 请求发送到 /doSomething 并接收槽生成的响应。

整个库都有完整的文档记录,并附带一个相当详尽的测试套件。它是跨平台的,并在 Linux、Windows 和 Mac OS X 上经过正式测试和支持。至少有一个主要的开源应用程序使用 QHttpEngine,NitroShare

I have just released the first version of QHttpEngine, which aims to fill the gap that you've described. The goal of the project is to provide an extremely simple set of classes that provide an HTTP server that integrates with Qt.

For example, to serve static files from resources in your application, all you would need to do is:

QFilesystemHandler handler(":/www");
QHttpServer server(&handler);

server.listen(QHostAddress::LocalHost, 8000);

You can also create a class derived from QObject that expose its slots as endpoints in an HTTP API. For example:

class ApiHandler : public QObjectHandler
{
    Q_OBJECT

private slots:

    QVariantMap doSomething(const QVariantMap ¶ms) {
        // do something with the parameters and return a response
    }
};

Users can then send a POST request to /doSomething with the parameters encoded as JSON and receive the response that the slot generates.

The entire library is fully documented and comes with a rather exhaustive test suite. It is cross-platform and is officially tested and supported on Linux, Windows, and Mac OS X. There is at least one major open-source application using QHttpEngine, NitroShare.

君勿笑 2024-09-14 11:29:36

这是一个非常简单的 HTTP Web 服务器,自从在 Web 浏览器上建立连接以来,它将每秒更新秒数。
它还在屏幕上显示网络浏览器发送的数据。
该程序设置为使用端口8080
例如127.0.0.1:8080

#-------------- Project file webServer3.pro ------- 
QT       += core
QT       += network
QT       -= gui

TARGET = webServer3
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

HEADERS += \
myhttpserver.h

/*------------------header file myhttpserver.h --------------*/ 
#ifndef MYHTTPSERVER
#define MYHTTPSERVER
#include <QCoreApplication>
#include <QNetworkInterface>
#include <iostream>
#include <QObject>
#include <QTcpSocket>
#include <QTcpServer>
#include <QDebug>

class myHTTPserver : public QObject
{
    Q_OBJECT
public:
    explicit myHTTPserver(QObject *parent = 0);
    ~myHTTPserver();
    QTcpSocket *socket ;
public slots:
    void myConnection();
private:
    qint64 bytesAvailable() const;
    QTcpServer *server;
signals:
};


/*------------------------main.cpp -------------------------*/
#include "myhttpserver.h"

using namespace std;
void delayms( int millisecondsToWait );

int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    myHTTPserver server;
    return a.exec();
}

myHTTPserver::myHTTPserver(QObject *parent) : QObject(parent)
    {
     server = new QTcpServer(this);
    // waiting for the web brower to make contact,this will emit signal
    connect(server, SIGNAL(newConnection()),this, SLOT(myConnection()));
    if(!server->listen(QHostAddress::Any,8080))cout<< "\nWeb server      could not start";
    else cout<<"\nWeb server is waiting for a connection on port 8080";
}

void myHTTPserver::myConnection()
    {
    static qint16 count;  //count number to be displayed on web browser
    socket = server->nextPendingConnection();
    while(!(socket->waitForReadyRead(100)));  //waiting for data to be read from web browser

    char webBrowerRXData[1000];
    int sv=socket->read(webBrowerRXData,1000);
    cout<<"\nreading web browser data=\n";
    for(int i=0;i<sv;i++)cout<<webBrowerRXData[i];
    cout<<"\n";

    socket->write("HTTP/1.1 200 OK\r\n");       // \r needs to be before \n
    socket->write("Content-Type: text/html\r\n");
    socket->write("Connection: close\r\n");
    socket->write("Refresh: 1\r\n\r\n");     //refreshes web browser     every second. Require two \r\n.

    socket->write("<!DOCTYPE html>\r\n");
    socket->write("<html><body>Number of seconds since connected.. ");
    QByteArray str;
    str.setNum(count++);   //convert int to string
    socket->write(str);
    socket->write(" </body>\n</html>\n");

    socket->flush();
    connect(socket, SIGNAL(disconnected()),socket, SLOT(deleteLater()));
    socket->disconnectFromHost();
}

myHTTPserver::~myHTTPserver()
    {
    socket->close();
}

Here is a very simple HTTP web server that will update the number of seconds, each second, since a connection was made on the web browser.
It also displays the data sent by the web browser on the screen.
The program is set to use port 8080
e.g 127.0.0.1:8080

#-------------- Project file webServer3.pro ------- 
QT       += core
QT       += network
QT       -= gui

TARGET = webServer3
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

HEADERS += \
myhttpserver.h

/*------------------header file myhttpserver.h --------------*/ 
#ifndef MYHTTPSERVER
#define MYHTTPSERVER
#include <QCoreApplication>
#include <QNetworkInterface>
#include <iostream>
#include <QObject>
#include <QTcpSocket>
#include <QTcpServer>
#include <QDebug>

class myHTTPserver : public QObject
{
    Q_OBJECT
public:
    explicit myHTTPserver(QObject *parent = 0);
    ~myHTTPserver();
    QTcpSocket *socket ;
public slots:
    void myConnection();
private:
    qint64 bytesAvailable() const;
    QTcpServer *server;
signals:
};


/*------------------------main.cpp -------------------------*/
#include "myhttpserver.h"

using namespace std;
void delayms( int millisecondsToWait );

int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    myHTTPserver server;
    return a.exec();
}

myHTTPserver::myHTTPserver(QObject *parent) : QObject(parent)
    {
     server = new QTcpServer(this);
    // waiting for the web brower to make contact,this will emit signal
    connect(server, SIGNAL(newConnection()),this, SLOT(myConnection()));
    if(!server->listen(QHostAddress::Any,8080))cout<< "\nWeb server      could not start";
    else cout<<"\nWeb server is waiting for a connection on port 8080";
}

void myHTTPserver::myConnection()
    {
    static qint16 count;  //count number to be displayed on web browser
    socket = server->nextPendingConnection();
    while(!(socket->waitForReadyRead(100)));  //waiting for data to be read from web browser

    char webBrowerRXData[1000];
    int sv=socket->read(webBrowerRXData,1000);
    cout<<"\nreading web browser data=\n";
    for(int i=0;i<sv;i++)cout<<webBrowerRXData[i];
    cout<<"\n";

    socket->write("HTTP/1.1 200 OK\r\n");       // \r needs to be before \n
    socket->write("Content-Type: text/html\r\n");
    socket->write("Connection: close\r\n");
    socket->write("Refresh: 1\r\n\r\n");     //refreshes web browser     every second. Require two \r\n.

    socket->write("<!DOCTYPE html>\r\n");
    socket->write("<html><body>Number of seconds since connected.. ");
    QByteArray str;
    str.setNum(count++);   //convert int to string
    socket->write(str);
    socket->write(" </body>\n</html>\n");

    socket->flush();
    connect(socket, SIGNAL(disconnected()),socket, SLOT(deleteLater()));
    socket->disconnectFromHost();
}

myHTTPserver::~myHTTPserver()
    {
    socket->close();
}
酷炫老祖宗 2024-09-14 11:29:36

QttpServer = Qt + libuv + REST = 您的 API 服务器

对于 需要处理大量并发连接的 Qt API 服务器 、请求以及你有什么...我们有 libuv 库。

尽管 libuv 是为 NodeJS 构建的,但 Qt 领域的粉丝仍然可以受益于 epoll 和 kqueues,而不是默认的 select 方法。

在 github 上查看更多内容 https://github.com/supamii/QttpServer

QttpServer = Qt + libuv + REST = your API server

For an Qt API server that need to handle a large number of concurrent connections, request, and what-have-you... we have the libuv library.

Though libuv is built for NodeJS, we fans in Qt-land can still benefit from epolls and kqueues instead of the default select method.

See more on github https://github.com/supamii/QttpServer

挽袖吟 2024-09-14 11:29:36

QhttpServer 似乎完全符合您的需要。
不幸的是它似乎不是很活跃。

QhttpServer seems to do exactly what you need.
unfortunately it does not seem to be very active.

悟红尘 2024-09-14 11:29:36

这是可以从 QML、纯 JS HTTP 处理中使用的简单 Qt 服务器: https://github.com /ncp1402/ql-服务器

Here is simple Qt server that can be used from QML, pure-JS HTTP processing: https://github.com/ncp1402/ql-server

(り薆情海 2024-09-14 11:29:36

上面的简单服务器在 MSwindows 操作系统上运行时存在问题,因为它会在一段时间后锁定。这是由于 waitForReadyRead() 在 MS 系统上以随机方式运行。 waitForReadyRead() 下面的程序被使用readyRead() 信号的事件循环替换。

# Project file
QT       += core
QT       += network
QT       -= gui
TARGET = webServer
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
HEADERS += myhttpserver.h

//-----------myhttpserver.h--------------   
#ifndef MYHTTPSERVER
#define MYHTTPSERVER
#include <QCoreApplication>
#include <iostream>
#include <QObject>
#include <QTcpSocket>
#include <QTcpServer>
#include <QIODevice>

class myHTTPserver : public QObject
{
    Q_OBJECT
public:
    explicit myHTTPserver(QObject *parent = 0);
    ~myHTTPserver();
    QTcpSocket *socket ;
public slots:
    void myConnection();
    void txRx();
    void closingClient();
private:
    qint64 bytesAvailable() const;
    QTcpServer *server;
};
#endif // MYHTTPSERVER

//------------------------ main.cpp ----------------------  
#include "myhttpserver.h"

using namespace std;

int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    myHTTPserver server;
    return a.exec();
}

myHTTPserver::myHTTPserver(QObject *parent) : QObject(parent)
    {
    server = new QTcpServer(this);
    connect(server, SIGNAL(newConnection()),this, SLOT(myConnection()));
    if(!server->listen(QHostAddress::Any,8080))cout<< "\nWeb server     could not start";
    else cout<<"\nWeb server is waiting for a connection on port 8080";
}

void myHTTPserver::myConnection()
    {
    socket = server->nextPendingConnection();
    connect(socket, SIGNAL(readyRead()), this, SLOT(txRx()));
    connect(socket, SIGNAL(disconnected()), this, SLOT(closingClient()));
}
void myHTTPserver::txRx()
    {
    char webBrowerRXData[1000];
    int sv=socket->read(webBrowerRXData,1000);
    cout<<"\nreading web browser data\n";
    for(int i=0;i<sv;i++)cout<<webBrowerRXData[i];
    cout<<"\n";

    socket->write("HTTP/1.1 200 OK\r\n");       // \r needs to be before \n
    socket->write("Content-Type: text/html\r\n");
    socket->write("Connection: close\r\n");
    socket->write("Refresh: 1\r\n");     //refreshes web browser every second. Require two \r\n.
    socket->write("Pragma: no-cache\r\n");
    socket->write("\r\n");
    socket->write("<!DOCTYPE html>\r\n");
    socket->write("<html><body>Number of seconds since connected.. ");
    QByteArray str;
    static qint16 count;  //count number to be displayed on web browser
    str.setNum(count++);   //convert int to string
    socket->write(str);
    socket->disconnectFromHost();
}

void myHTTPserver::closingClient()
    {
        socket->deleteLater();
}

myHTTPserver::~myHTTPserver()
    {
    cout<<"\nclosing socket\n";
    socket->close();
}

The above simple server had issues running on MSwindows operating systems as it would lockup after a while. This was due to waitForReadyRead() operating in a random fashion on MS systems. The program below waitForReadyRead() was replaced with an event loop using readyRead() signal.

# Project file
QT       += core
QT       += network
QT       -= gui
TARGET = webServer
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
HEADERS += myhttpserver.h

//-----------myhttpserver.h--------------   
#ifndef MYHTTPSERVER
#define MYHTTPSERVER
#include <QCoreApplication>
#include <iostream>
#include <QObject>
#include <QTcpSocket>
#include <QTcpServer>
#include <QIODevice>

class myHTTPserver : public QObject
{
    Q_OBJECT
public:
    explicit myHTTPserver(QObject *parent = 0);
    ~myHTTPserver();
    QTcpSocket *socket ;
public slots:
    void myConnection();
    void txRx();
    void closingClient();
private:
    qint64 bytesAvailable() const;
    QTcpServer *server;
};
#endif // MYHTTPSERVER

//------------------------ main.cpp ----------------------  
#include "myhttpserver.h"

using namespace std;

int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    myHTTPserver server;
    return a.exec();
}

myHTTPserver::myHTTPserver(QObject *parent) : QObject(parent)
    {
    server = new QTcpServer(this);
    connect(server, SIGNAL(newConnection()),this, SLOT(myConnection()));
    if(!server->listen(QHostAddress::Any,8080))cout<< "\nWeb server     could not start";
    else cout<<"\nWeb server is waiting for a connection on port 8080";
}

void myHTTPserver::myConnection()
    {
    socket = server->nextPendingConnection();
    connect(socket, SIGNAL(readyRead()), this, SLOT(txRx()));
    connect(socket, SIGNAL(disconnected()), this, SLOT(closingClient()));
}
void myHTTPserver::txRx()
    {
    char webBrowerRXData[1000];
    int sv=socket->read(webBrowerRXData,1000);
    cout<<"\nreading web browser data\n";
    for(int i=0;i<sv;i++)cout<<webBrowerRXData[i];
    cout<<"\n";

    socket->write("HTTP/1.1 200 OK\r\n");       // \r needs to be before \n
    socket->write("Content-Type: text/html\r\n");
    socket->write("Connection: close\r\n");
    socket->write("Refresh: 1\r\n");     //refreshes web browser every second. Require two \r\n.
    socket->write("Pragma: no-cache\r\n");
    socket->write("\r\n");
    socket->write("<!DOCTYPE html>\r\n");
    socket->write("<html><body>Number of seconds since connected.. ");
    QByteArray str;
    static qint16 count;  //count number to be displayed on web browser
    str.setNum(count++);   //convert int to string
    socket->write(str);
    socket->disconnectFromHost();
}

void myHTTPserver::closingClient()
    {
        socket->deleteLater();
}

myHTTPserver::~myHTTPserver()
    {
    cout<<"\nclosing socket\n";
    socket->close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文