`
guoyiqi
  • 浏览: 1001856 次
社区版块
存档分类
最新评论

C语言cgi(1)

 
阅读更多

1
Columbia University
cs3157 – Advanced Programming
Summer 2014, Lab #2, 60ish points
June 9, 2014
Follow these step-by-step instructions. This lab must be submitted electronically by Monday June
16th, 11:55 pm.
The point of the labs is to teach and reinforce specific programming concepts, cut time off your
homework assignment while trying to maintain your interest. If you find them tedious or pointless, please
don’t hesitate to bring it to your instructor’s attention (the same applies the opposite).
Step 1. Visual Editor and Debugging (5 points)
A debugger is a program that allows you (the programmer) to step through an executing program (i.e live,
real time) and track internal information and monitor progress. This allows you to monitor logical
progress and also track down errors.
Perldoc perldebug
Will give you more information about the debugging environment. Executing the perl command with the
-d flag will start perl in debugging mode. Try it….take one of the scripts from lab1 and execute it
perl -d something.pl
(h gives you help, q to quit, v shows you some code around you, etc).
The nice part about eclipse is the ability of running the debugger in a visual environment, so that you
don’t need to memorize random key commands. (That is a good thing and bad). You have a choice if you
want to run it from the command line or visual environment. Be aware that certain specific versions of
eclipse (older) and certain versions of EPIC (older) have issues with the debug system, which causes it to
work every other time J
Write up a paragraph or two about your experience in running the debugging environment on one of the
lab scripts from last lab and over the lab 2 assignments. Answer the following questions after you are
done with the lab:
1) After using the debugger, did your code work become easier or harder ?
2) Any issues with the perl debugger in the clic lab ?
3) After using it to watch a variable what can you say about perl variable scope ?
4) What is the difference between stepping over and stepping through in regards to subs ?
5) How does the average rainfall in the Sahara affect Perl development ?
Step 2 (20 points)
Create a perl file called step2.pl. I am providing a file called “testip” to be used as a test file.
2
Background information: Computer on the internet are identified using a few standard conventions.
One of them are IP address. The format for IP version 4, is 4 sets of numbers between 0-255. So your own
machine might be 128.59.18.1 etc
1) Given a specific file (passed as a command line argument (important) ) which contains among
other things, IP addresses, I would like you to locate and count
any IP address in the file. For example the ip address 255.234.255.1 (4 numbers separated by a
period).
NOTE: you need to make sure none of the numbers are not larger than 255
2) I would also like a printout of all the IP addresses, and the number of times it was seen in the file.
Addresses can appear multiple times on a line…keep that in mind.
3) I would like to know how much time has passed from the beginning and end of the program run,
we would like to see how much time it took to analyze. Lookup localtime command to see how to
grab the current time.
You will need to loop through the file, find the ip address (use the regular expressions covered in class on
Monday), and then keep a count of number of each one seen. Once done, you can print them out in sorted
order in the following format:
Processed file YYYYY
Number of unique IP: 3
123.123.123.123 9
12.12.112.2 7
23.125.123.256 2
..
File analyzed in: 1 minute and 23 seconds
Step 3: (4 points)
Consider the following Perl program:
#!/usr/bin/perl
use strict;
my(@list) = (5, "Todd", 3, 2);
$list[2] += 4;
print "M1: ".$list[0]." ".$list[1]." ".$list[2]." ".$list[3]."\n";
($list[1], $list[3]) = funky(@list);
print "M2: ".$list[0]." ".$list[1]." ".$list[2]." ".$list[3]."\n";
## End of main program
sub funky {
my($v1, $v2, $v3) = @_;
print "F1: "." ".$v1." ".$v2." ".$v3.
" ".$_[0]." ".$_[1]." ".$_[2]." ".$_[3]."\n";
$_[0]++;
print "F2: "." ".$v1." ".$v2." ".$v3.
" ".$_[0]." ".$_[1]." ".$_[2]." ".$_[3]."\n";
$v3 = "Bill";
print "F3: "." ".$v1." ".$v2." ".$v3.
" ".$_[0]." ".$_[1]." ".$_[2]." ".$_[3]."\n";
3
return($v3, $v2);
} ## Of function FUNKY
What did you expect the output to look like and what did it actually look like ? Explain (based on what
we covered in class) what is going on…
Step 4 New Process monitor (20 points)
As demo-ed in class, you will write a simple process monitor. You will use the output of the (ps –aux)
command to pick out all the running pid->program names every 5 seconds. You will run your program for
10 minutes printing out when a program starts, or stops.
As we discussed you will need to run the ps command and then create a map list of the current running
pid and process names. You will compare current to last map (so if its at 10 seconds, you need to look
back at 5 seconds) for any processes (pid) which are new to say process is new. Use the old one to look at
the new one to detect dead process id.
Most of the code was demo-ed in class, type it up and add comments.
Step 5: Moving information over CGI Intro (4 points)
We started to talk about CGI in the last class. Forms are simple ways of feeding information to the user
and having them interact with your script in some way.
Here is your chance to start to play around with it. Create a HTML page (feel free to use eclipse or your
favorite text editor) (step5.html) should have the following code in it:
<html><head><title>Step2 webpage</title></head>
<body>
<form action=”step5a.pl.cgi” method=POST>
Enter something:
<input type=”text” name=”t1”>
<input type=”submit” value=”testpost”></form>
<HR>
<form action=”step5b.pl.cgi” method=GET>
Enter Some text:
<input type=”text” name=”t2”>
<input type=”submit” value=”testget”></form>
</body>
</html>
step5a.pl.cgi and step5b.pl.cgi should look like:
use strict;
4
my $time = localtime;
print "Content-type: text/html\n\n";
print <<END_OF_PRINTING;
This is the time : $time
END_OF_PRINTING
foreach my $vars (sort keys %ENV){
print "<b>$vars</b>=";
print "$ENV{$vars}<BR>";
}
Describe in a short paragraph what you see is the difference between get and post when running the
cgi script. Do a quick search and read up on get and post in a general sense. Which do you think is a better
way to work with ??
Note: the webserver has perl in /usr/bin/perl
Step 6: More CGI and more basic practice (5 points):
Copy and adopt the previous html file to a new file called Step6.html. Have it call step6a.pl.cgi and
step6b.pl.cgi . In both cases you need to process and display the input passed in, as a simple html page
saying
I was given Key = …. With a value of = …
(for both input types).
Hint: This means you need to parse out the arguments passed in. remember to first split around the & sign
and then around the equal sign. Example:
my ($pairs,$key,value);
foreach $pairs (split /&/ $inputstring){
#now cut the $pairs around the equal sign …..
Hint2: one of the methods passes user input through ENV and one passes it through STDIN, but you still
ENV for the length (see read method in perl)
HINT3: the scripts are basically the same, get one to work and then copy and convert it for the other task.

分享到:
评论

相关推荐

    C语言CGI编程

    ### C语言CGI编程知识点详解 #### 一、公共网关接口(CGI)概述 公共网关接口(Common Gateway Interface,简称CGI)是一种用于Web服务器与外部应用程序之间的通信标准,它允许Web服务器调用外部程序处理客户端的...

    C语言写CGI程序详细教程

    C语言写CGI程序详细教程 CGI(Common Gateway Interface,公共网关接口)是一种标准的web服务器和脚本程序之间的接口协议。CGI程序可以用任何编程语言编写,如Shell脚本语言、Perl、Fortran、Pascal、C语言等。但是...

    C语言CGI编写WEB查询系统

    ### C语言CGI编写WEB查询系统 #### 一、CGI概述 公共网关界面(Common Gateway Interface,简称CGI)是一种编程规范,它定义了Web服务器如何与外部应用程序(即CGI程序)进行交互的标准接口。通过CGI,Web服务器...

    C语言CGI的学习资料

    **C语言CGI(Common Gateway Interface)学习指南** CGI,即通用网关接口,是一种标准,用于将Web服务器与各种脚本语言或应用程序连接起来,以动态生成网页内容。在C语言中实现CGI,可以让你创建更复杂的Web应用...

    用C语言进行CGI程序设计

    在本课程设计中,使用C语言编写CGI程序是为了实现一个特定的应用场景:通过网页表单收集用户输入的运算结果,并在后台处理这些数据,然后将结果返回给用户。 C语言被选择作为CGI程序的开发语言,主要得益于它的高效...

    C语言CGI商业程序,可上传图片存入mysql数据库

    总结来说,"C语言CGI商业程序,可上传图片存入mysql数据库"是一个结合了C语言编程、CGI技术和MySQL数据库的Web应用解决方案。它提供了一种方式让用户通过Web界面上传图片,并将这些图片的信息安全地存储在MySQL...

    嵌入式系统C语言CGI设计与应用.pdf

    总结来说,嵌入式系统中C语言CGI的设计与应用是网络远程控制技术中一个重要的领域。通过CGI技术,可以实现对嵌入式系统网络接口的远程管理与控制,其优点在于技术成熟、语言无关且具有较好的平台兼容性。在具体的...

    用C语言编写CGI程序

    下面是一个简单的C语言CGI程序示例,用于演示如何处理表单数据: ```c #include #include int main() { int i, n; printf("Content-type: text/plain\n\n"); if (getenv("CONTENT_LENGTH")) { n = atoi...

    c语言多用户登录功能cgi

    c语言编写的多用户登录功能cgi程序,适用于嵌入式,需编译。需要在程序同目录下创建文件user.txt并写入用户名密码用于验证,格式为:用户名空格密码空格用户名空格密码

    c语言写的CGI 计数器

    c语言写的CGI 计数器 在linux 下运行的,要自己加一个count.dat的文件,不然会访问失败的。

    c语言编写的cgi程序

    1. **CGI原理**:CGI程序充当了Web服务器和客户端之间的一个桥梁,当用户通过浏览器发送HTTP请求时,CGI程序会解析这些请求并生成相应的响应。在GET方式下,参数会被编码在URL中。 2. **C语言编程**:C语言是一种...

    cgi-util C语言写CGI库

    1. **请求解析**:`cgi-util`能够解析HTTP请求头,提取如GET、POST参数、cookies等信息。这使得开发者可以轻松地获取并处理来自客户端的数据。 2. **输出处理**:库提供函数来设置HTTP响应头,如Content-Type、...

    C语言编写的CGI开发库(linux)

    1. **CGILib初始化**:在开始处理CGI请求之前,需要调用`cgi_init()`函数来初始化库,这会处理环境变量和HTTP请求头。 2. **请求数据解析**:库提供API如`cgi_get_parameter()`用于获取HTTP请求中的参数值,`cgi_...

    c语言 cgi 示例

    C语言CGI程序的核心部分通常包括以下步骤: 1. **环境变量解析**:CGI程序首先需要获取来自HTTP请求的环境变量,如QUERY_STRING(GET方法传递的参数)、REQUEST_METHOD(GET或POST请求类型)和CONTENT_TYPE(POST...

    用c语言进行cgi程序设计.rar_CGI程序设计_cgi_cgi c语言

    在本资源中,我们探讨的是如何使用C语言来编写CGI程序。 C语言是一种强大且高效的编程语言,适用于系统级编程和网络应用。在CGI编程中,C语言能够提供对底层操作系统接口的直接访问,使得开发者可以精确控制程序的...

    C语言开发CGI初步了解

    3. **C语言CGI示例**:以下是一个简单的C语言CGI程序示例,用于展示如何接收表单数据并输出结果: ```c #include #include int main() { char *query_string = getenv("QUERY_STRING"); if (query_string !=...

    c语言开发的cgi简单论坛源码

    【标题】:“C语言开发的CGI简单论坛源码”是一个基于C语言实现的CGI(Common Gateway Interface)应用程序,用于构建一个基本的在线论坛。这个项目是学习CGI编程和C语言结合的理想实例,因为它提供了对用户管理、...

    CGI之C语言篇.pdf

    以下是一个简单的C语言CGI程序示例: ```c #include #include int main(void) { char *data; long m, n; // 设置HTTP响应头 printf("%s%c%c", "Content-Type:text/html;charset=gb2312", 13, 10); printf...

Global site tag (gtag.js) - Google Analytics