Cgi koodinäiteid: erinevus redaktsioonide vahel
Mine navigeerimisribale
Mine otsikasti
Resümee puudub |
Resümee puudub |
||
| (ei näidata sama kasutaja 9 vahepealset redaktsiooni) | |||
| 1. rida: | 1. rida: | ||
Shelli skript, mis kuvab kataloogi listingu: | Shelli skript, mis kuvab kataloogi listingu: | ||
< | <pre> | ||
#!/bin/sh | #!/bin/sh | ||
echo 'content-type: text/html' | echo 'content-type: text/html' | ||
echo | echo | ||
echo ' uus ok leht olemas ' | echo ' uus ok leht olemas ' | ||
ls -l | ls -l | ||
</pre> | </pre> | ||
Pythoni skript, mis trükib ühe sõna kolm korda: | Pythoni skript, mis trükib ühe sõna kolm korda: | ||
< | <pre> | ||
#!/usr/bin/python | #!/usr/bin/python | ||
| 24. rida: | 21. rida: | ||
foo() | foo() | ||
foo() | foo() | ||
</pre> | |||
C "hello world" programm, mis tuleks kompileerida. | |||
<pre> | |||
#include <stdio.h> | |||
int main(void) { | |||
printf("content-type: text/html\n\n"); | |||
printf("Tere ise ka!\n"); | |||
/* | |||
fflush(stdout); | |||
exit(0); | |||
*/ | |||
} | |||
</pre> | |||
Shelli skript, mis kuvab etteantud cgi parameetrid: | |||
<pre> | |||
#!/bin/sh | |||
echo 'content-type: text/html' | |||
echo | |||
echo ' uus ok leht olemas ' | |||
echo $QUERY_STRING | |||
</pre> | |||
C näide, mis liidab kahte arvu (NB! csapp.h tuleb võtta tiny veebiserverist, vt kursuse lehel tiny.zip) | |||
<pre> | |||
/* | |||
* adder.c - a minimal CGI program that adds two numbers together | |||
*/ | |||
#include "csapp.h" | |||
int main(void) { | |||
char *buf, *p; | |||
char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE]; | |||
int n1=0, n2=0; | |||
/* Extract the two arguments */ | |||
if ((buf = getenv("QUERY_STRING")) != NULL) { | |||
p = strchr(buf, '&'); | |||
*p = '\0'; | |||
strcpy(arg1, buf); | |||
strcpy(arg2, p+1); | |||
n1 = atoi(arg1); | |||
n2 = atoi(arg2); | |||
} | |||
/* Make the response body */ | |||
sprintf(content, "Welcome to add.com: "); | |||
sprintf(content, "%sTHE Internet addition portal.\r\n<p>", content); | |||
sprintf(content, "%sThe answer is: %d + %d = %d\r\n<p>", | |||
content, n1, n2, n1 + n2); | |||
sprintf(content, "%sThanks for visiting!\r\n", content); | |||
/* Generate the HTTP response */ | |||
// printf("content-length: %d\r\n", strlen(content)); | |||
printf("content-type: text/html\n\n"); | |||
printf("%s", content); | |||
fflush(stdout); | |||
exit(0); | |||
} | |||
</pre> | |||
Shelli skript, mis paneb käima java rakenduse (klassi nimega proov) Taneli läpakas: teistel on vaja programmide/failide asukohad muuta enda masina jaoks sobivaks, samuti pane java käsk ühe rea peale, mitte kahele reale, nagu allpool!!! | |||
NB! IT Kolledzhis on vaja enne java käsu rida skripti kirjutada lisaks | |||
cd /tmp | |||
Miks see vajalik on, ei ole hetkel teada. | |||
<pre> | |||
#!/bin/sh | |||
/usr/local/java/j2sdk1.4.2_02/bin/java -cp /home/tanel/Lectures/Vorgurak1/Cgiproov/ | |||
proov $QUERY_STRING | |||
</pre> | |||
Java programmi näide, mida saab eelmise skriptiga käivitada | |||
<pre> | |||
import java.io.*; | |||
public class proov { | |||
public static void main(String[] args) { | |||
String ls_str; | |||
System.out.println("content-type: text/html"); | |||
System.out.println(""); | |||
System.out.println("<p>OK algus javast on siin</p>"); | |||
System.out.println(args[0]); | |||
} | |||
} | |||
</pre> | </pre> | ||
Viimane redaktsioon: 8. märts 2006, kell 15:02
Shelli skript, mis kuvab kataloogi listingu:
#!/bin/sh echo 'content-type: text/html' echo echo ' uus ok leht olemas ' ls -l
Pythoni skript, mis trükib ühe sõna kolm korda:
#!/usr/bin/python def foo(): print "<p>miskijutt</p>" print "content-type: text/html" print print "ok !! jalle olemas" foo() foo() foo()
C "hello world" programm, mis tuleks kompileerida.
#include <stdio.h>
int main(void) {
printf("content-type: text/html\n\n");
printf("Tere ise ka!\n");
/*
fflush(stdout);
exit(0);
*/
}
Shelli skript, mis kuvab etteantud cgi parameetrid:
#!/bin/sh echo 'content-type: text/html' echo echo ' uus ok leht olemas ' echo $QUERY_STRING
C näide, mis liidab kahte arvu (NB! csapp.h tuleb võtta tiny veebiserverist, vt kursuse lehel tiny.zip)
/*
* adder.c - a minimal CGI program that adds two numbers together
*/
#include "csapp.h"
int main(void) {
char *buf, *p;
char arg1[MAXLINE], arg2[MAXLINE], content[MAXLINE];
int n1=0, n2=0;
/* Extract the two arguments */
if ((buf = getenv("QUERY_STRING")) != NULL) {
p = strchr(buf, '&');
*p = '\0';
strcpy(arg1, buf);
strcpy(arg2, p+1);
n1 = atoi(arg1);
n2 = atoi(arg2);
}
/* Make the response body */
sprintf(content, "Welcome to add.com: ");
sprintf(content, "%sTHE Internet addition portal.\r\n<p>", content);
sprintf(content, "%sThe answer is: %d + %d = %d\r\n<p>",
content, n1, n2, n1 + n2);
sprintf(content, "%sThanks for visiting!\r\n", content);
/* Generate the HTTP response */
// printf("content-length: %d\r\n", strlen(content));
printf("content-type: text/html\n\n");
printf("%s", content);
fflush(stdout);
exit(0);
}
Shelli skript, mis paneb käima java rakenduse (klassi nimega proov) Taneli läpakas: teistel on vaja programmide/failide asukohad muuta enda masina jaoks sobivaks, samuti pane java käsk ühe rea peale, mitte kahele reale, nagu allpool!!!
NB! IT Kolledzhis on vaja enne java käsu rida skripti kirjutada lisaks
cd /tmp
Miks see vajalik on, ei ole hetkel teada.
#!/bin/sh
/usr/local/java/j2sdk1.4.2_02/bin/java -cp /home/tanel/Lectures/Vorgurak1/Cgiproov/
proov $QUERY_STRING
Java programmi näide, mida saab eelmise skriptiga käivitada
import java.io.*;
public class proov {
public static void main(String[] args) {
String ls_str;
System.out.println("content-type: text/html");
System.out.println("");
System.out.println("<p>OK algus javast on siin</p>");
System.out.println(args[0]);
}
}