html - 创建一个网页来演示 C 代码

标签 html c demo

我有一个 C 代码,它将一个文件作为输入,对其进行处理并给出一个数字作为输出。我想构建一个 html 网页,它将文件路径作为输入,并将其提供给 C 代码。 C 代码对其进行处理并在浏览器中显示输出(整数)。你能建议我该怎么做吗?是否有任何预构建的软件可以执行此操作?

最佳答案

如果 C 代码用于生成命令行实用程序,那么您可以在生成网页时调用它:

#!/usr/bin/env python
import subprocess
from bottle import request, route, run, template # http://bottlepy.org/

command = ['wc', '-c'] # <-- XXX put your command here

@route('/')
def index():
    filename = request.query.filename or 'default' # query: /?filename=<filename>
    output = subprocess.check_output(command + [filename]) # run the command
    return template("""<dl>
        <dt>Input</dt>
            <dd>{{filename}}</dd>
        <dt>Output</dt>
            <dd>{{output}}</dd></dl>""", filename=filename, output=output)

run(host='localhost', port=8080)

运行此脚本或将其粘贴到 Python 控制台中,然后打开浏览器并传递文件名(服务器上的路径)作为查询参数:

$ python -mwebbrowser http://localhost:8080/?filename=/etc/passwd

wc -c 打印每个输入文件的字节数。它在服务器上执行。

<小时/>

如果 C 代码可作为库使用;你可以使用 ctypes module从 Python 调用 C 函数,例如从 libc 库调用 printf() C 函数:

#!/usr/bin/env python
import ctypes
from ctypes.util import find_library

try:
    libc = ctypes.cdll.msvcrt # Windows
except OSError:
    libc = ctypes.cdll.LoadLibrary(find_library('c'))

n = libc.printf("abc ")
libc.printf("%d", n)

关于html - 创建一个网页来演示 C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21876969/

相关文章:

C:逻辑运算符

java - 如何在Android上创建透明演示?

html - go - 使用继承渲染 html/模板

html - 对齐 flex 盒几乎没有帮助

javascript - 创建可在音频播放器中播放的音乐的 HTML 下拉列表

html - IE8/Compat View Bug - 所有图像在 Div 顶部相互堆叠。需要帮忙

c - 编写一个函数来检查关系语句是真还是假

c++ - 当使用__block时,clang BlocksRuntime在可执行文件中嵌入 'obsolete compiler'警告

tensorflow - 如何在 tensorflow playground 的第 4 个数据集上实现 0(训练和测试)损失?

HTML 5 结构标签演示