Skip to content

Commit b6f770e

Browse files
committed
different examples
1 parent af308c7 commit b6f770e

File tree

19 files changed

+468
-0
lines changed

19 files changed

+468
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Structure
2+
3+
project
4+
├── README.md
5+
├── cgi-bin
6+
│   ├── image.py
7+
│   ├── test.php
8+
│   ├── test.pl
9+
│   ├── test.py
10+
│   ├── test.rb
11+
│   ├── test.sh
12+
│   └── test2.php
13+
├── images
14+
│   ├── normal.png
15+
│   └── transparent.png
16+
├── index.html
17+
└── server.py
18+
19+
# Simple CGI Server
20+
21+
Normally you can start
22+
23+
python3 -m http.server --cgi 8000
24+
25+
and it will serve directly all files in current folder and subolfers but it will send source code for scripts. You have to put scripts in `cgi-bin` folder
26+
27+
On Linux you have to set `execution attribut` for every script (ie. `chmod +x script.py`) and you have to use `shebang` in first line of script (ie. `#!/usr/bin/env python3`) because Linux doesn't care of file extensions (ie, `.py`) and it can run script without extension or with incorrect extension (ie. `script.png`)
28+
Lines changed: 13 additions & 0 deletions
Loading
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
import os
5+
6+
src = "images/normal.png"
7+
8+
sys.stdout.write("Content-Type: image/png\n")
9+
sys.stdout.write("Content-Length: " + str(os.stat(src).st_size) + "\n")
10+
sys.stdout.write("\n")
11+
sys.stdout.flush()
12+
sys.stdout.buffer.write(open(src, "rb").read())
13+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test: test.c
2+
gcc -o test test.c
3+
4+
8.42 KB
Binary file not shown.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <stdio.h>
2+
3+
int main()
4+
{
5+
int result;
6+
7+
printf("Content-Type: text/html; charset=utf-8\n");
8+
printf("\n");
9+
10+
/* "Content-Type" has to be in first line - without even empty lines */
11+
/* after headers have to be empty line */
12+
13+
result = 1 + 2;
14+
15+
printf("<!DOCTYPE html>\n\
16+
\n\
17+
<html>\n\
18+
\n\
19+
<head>\n\
20+
<meta charset=\"utf-8\"/>\n\
21+
<title>C</title>\n\
22+
<style>\n\
23+
body {background-color:#ddd; text-align: center}\n\
24+
</style>\n\
25+
</head>\n\
26+
\n\
27+
<body>\n\
28+
\n\
29+
<h1>C</h1>\n\
30+
\n\
31+
<h2>1 + 2 = %i</h2>\n\
32+
\n\
33+
<a href=\"/images/transparent.png\"><img src=\"/images/transparent.png\"><br/>/images/transparent.png</a><br/>\n\
34+
<br/>\n\
35+
\n\
36+
<a href=\"/cgi-bin/image.png\"><img src=\"/cgi-bin/image.png\"><br/>/cgi-bin/image.png</a><br/>\n\
37+
(python script with extension .png)<br/>\n\
38+
\n\
39+
</body>\n\
40+
\n\
41+
</html>", result);
42+
43+
return 0;
44+
}
45+
46+
/* it needs ' \" ' */
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env perl
2+
print "Content-Type: text/html; charset=utf-8\n";
3+
print "\n";
4+
5+
# "Content-Type" has to be in first line - without even empty lines
6+
# after headers have to be empty line
7+
8+
$result = 1 + 2;
9+
10+
print <<ENDHTML
11+
<!DOCTYPE html>
12+
13+
<html>
14+
15+
<head>
16+
<meta charset="utf-8"/>
17+
<title>Perl</title>
18+
<style>
19+
body {background-color:#ddd; text-align: center}
20+
</style>
21+
</head>
22+
23+
<body>
24+
25+
<h1>PERL</h1>
26+
27+
<h2>1 + 2 = $result</h2>
28+
29+
<a href="/images/transparent.png"><img src="/images/transparent.png"><br/>/images/transparent.png</a><br/>
30+
<br/>
31+
32+
<a href="/cgi-bin/image.png"><img src="/cgi-bin/image.png"><br/>/cgi-bin/image.png</a><br/>
33+
(python script with extension .png)<br/>
34+
35+
</body>
36+
37+
</html>
38+
ENDHTML
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env python3
2+
print("Content-Type: text/html; charset=utf-8")
3+
print()
4+
5+
# "Content-Type" has to be in first line - without even empty lines
6+
# after headers have to be empty line (another print() or "\n")
7+
8+
result = 1 + 2
9+
10+
print('''<!DOCTYPE html>
11+
12+
<html>
13+
14+
<head>
15+
<meta charset="utf-8"/>
16+
<title>Python</title>
17+
<style>
18+
body {{background-color:#ddd; text-align: center}}
19+
</style>
20+
</head>
21+
22+
<body>
23+
24+
<h1>PYTHON</h1>
25+
26+
<h2>1 + 2 = {}</h2>
27+
28+
<a href="/images/transparent.png"><img src="/images/transparent.png"><br/>/images/transparent.png</a><br/>
29+
<br/>
30+
31+
<a href="/cgi-bin/image.png"><img src="/cgi-bin/image.png"><br/>/cgi-bin/image.png</a><br/>
32+
(python script with extension .png)<br/>
33+
34+
</body>
35+
36+
</html>'''.format(result))
37+
38+
# style needs `{{ }}` instead of `{ }` because `format()` uses `{ }`
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env ruby
2+
puts "Content-Type: text/html; charset=utf-8\n"
3+
puts "\n"
4+
5+
# "Content-Type" has to be in first line - without even empty lines
6+
# after headers have to be empty line "\n"
7+
8+
result = 1 + 2
9+
10+
puts <<ENDHTML
11+
<!DOCTYPE html>
12+
13+
<html>
14+
15+
<head>
16+
<meta charset="utf-8"/>
17+
<title>Ruby</title>
18+
<style>
19+
body {background-color:#ddd; text-align: center}
20+
</style>
21+
</head>
22+
23+
<body>
24+
25+
<h1>RUBY</h1>
26+
27+
<h2>1 + 2 = #{result}</h2>
28+
29+
<a href="/images/transparent.png"><img src="/images/transparent.png"><br/>/images/transparent.png</a><br/>
30+
<br/>
31+
32+
<a href="/cgi-bin/image.png"><img src="/cgi-bin/image.png"><br/>/cgi-bin/image.png</a><br/>
33+
(python script with extension .png)<br/>
34+
35+
</body>
36+
37+
</html>
38+
ENDHTML
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/sh
2+
echo "Content-Type: text/html; charset=utf-8\n"
3+
echo "\n"
4+
5+
# "Content-Type" has to be in first line - without even empty lines
6+
# after headers have to be empty line "\n"
7+
8+
result=$((1+2))
9+
10+
echo '<!DOCTYPE html>
11+
12+
<html>
13+
14+
<head>
15+
<meta charset="utf-8"/>
16+
<title>Bash</title>
17+
<style>
18+
body {background-color:#ddd; text-align: center}
19+
</style>
20+
</head>
21+
22+
<body>
23+
24+
<h1>BASH</h1>
25+
26+
<h2>1 + 2 = '$result'</h2>
27+
28+
<a href="/images/transparent.png"><img src="/images/transparent.png"><br/>/images/transparent.png</a><br/>
29+
<br/>
30+
31+
<a href="/cgi-bin/image.png"><img src="/cgi-bin/image.png"><br/>/cgi-bin/image.png</a><br/>
32+
(python script with extension .png)<br/>
33+
34+
</body>
35+
36+
</html>'
37+
38+
# Bash can use variable only in " ",
39+
# but I use " " inside text so I need ' '

0 commit comments

Comments
 (0)