0

I have a C code which takes a file as input, processes it and gives a number as output. I want to build a html webpage which takes the file path as input, gives it to the C code. C code processes it and output(integer) is displayed in browser. Can you please suggest me how to go about this? Are there are any prebuilt softwares to do this?

Nick
  • 949
  • 1
  • 11
  • 31
  • 1
    If it's for a homework, read your textbook. If it's for private use, just don't do that. You will need to use an HTTP library and cope with all the tedium of low-level protocol handling to achieve the equivalent of a couple of lines of PHP. – kuroi neko Feb 19 '14 at 10:03
  • @kuroineko: there is nothing wrong with calling an external subprocess (the C code) or invoking it as a library to generate data for a web page (you could use any language to implement this part, including php). – jfs Feb 19 '14 at 10:08
  • @J.F.Sebastian Well given the imprecision of the question you could expect anything from CGI-bin to a full fledged HTTP client. My point is, unless you know exactly what you're doing, better not start tackling network programming problems with the wrong tool. – kuroi neko Feb 19 '14 at 10:12
  • Would the file be a local file or a file on the server? Do you want the C program to run on the client or server (there are security implications if you want to run it on the client). – Klas Lindbäck Feb 19 '14 at 10:15
  • hmmm, programming something that reads a file, process its contents and sends a response back via HTTP. If only such a thing existed: a server-side scripting language would be a very useful thing indeed. Stuff like asp, Perl, Python, PHP, node.js, ... [The list really is quite long](http://en.wikipedia.org/wiki/Server-side_scripting#Languages). Yes, [C + CGI](https://www.cs.tut.fi/~jkorpela/forms/cgic.html) works, too, but really: save yourself some time, and write script – Elias Van Ootegem Feb 19 '14 at 10:37

1 Answers1

1

If C code is used to produce a command line utility then you could call it while generating a web page:

#!/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)

Run this script or paste it into a Python console, then open your browser and pass a filename (a path on the server) as a query parameter:

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

wc -c prints number of bytes for each input file. It is executed on the server.


If C code is available as a library; you could use ctypes module to call a C function from Python e.g., to call printf() C function from libc library:

#!/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)
jfs
  • 399,953
  • 195
  • 994
  • 1,670