近期偶然玩了一下CGI,收集点资料写篇在这里留档。

如今想做HTTP Cache回归測试了,为了模拟不同的响应头及数据大小。就须要一个CGI按须要传回指定的响应头和内容。这是从老外的測试页面学习到的经验。

CGI事实上就是使用STDIN和环境变量作为输入,  STDOUT做为输出。按照Http协议生成相应的数据。

一. 数据输出

数据输出遵循HTTP协议,分为四部分:

  状态行 (Status Line):

     200 OK

  响应头(Response Headers):

     Content-Type: text/html

     Content-Length: 3072

   空白行(代表响应头已经列完, 仅仅能包括回车和换行符):

   

   数据(文本或二进制数据):

   <html>xxxxxx</html>

參考: HTTP Protocol

状态行和响应头中每一行都必须以\r\n(回车及换行)结束。使用这个规则,以简单的文本组装就能够完毕响应了。

仅仅要注意二进制数据输出方法就能够了。 尝试设定Content-Length时可能没办法设定的准确,至少对于Apache Server是这样,只是不影响最后返回的数据量。

二. 数据输入

CGI的数据输入主要是环境变量,以下这个链接有一串定义,

CGI Environment Variables

Key

Value

DOCUMENT_ROOT

The root directory of your server

HTTP_COOKIE

The visitor's cookie, if one is set

HTTP_HOST

The hostname of the page being attempted

HTTP_REFERER

The URL of the page that called your program

HTTP_USER_AGENT

The browser type of the visitor

HTTPS

"on" if the program is being called through a secure server

PATH

The system path your server is running under

QUERY_STRING

The query string (see GET, below)

REMOTE_ADDR

The IP address of the visitor

REMOTE_HOST

The hostname of the visitor (if your server has reverse-name-lookups on; otherwise this is the IP address again)

REMOTE_PORT

The port the visitor is connected to on the web server

REMOTE_USER

The visitor's username (for .htaccess-protected pages)

REQUEST_METHOD

GET or POST

REQUEST_URI

The interpreted pathname of the requested document or CGI (relative to the document root)

SCRIPT_FILENAME

The full pathname of the current CGI

SCRIPT_NAME

The interpreted pathname of the current CGI (relative to the document root)

SERVER_ADMIN

The email address for your server's webmaster

SERVER_NAME

Your server's fully qualified domain name (e.g. www.cgi101.com)

SERVER_PORT

The port number your server is listening on

SERVER_SOFTWARE

The server software you're using (e.g. Apache 1.3)

当你须要CGI处理POST请求时,CGI就要使用STDIN做为输入来接收数据了。

在Perl里使用以下的代码读取:

use CGI;

my $cgi = CGI->new();

my %params = $cgi->Vars();

而在Python则是:

import cgi

cgi.FieldStorage()

參考: Perl CGI Input Test

三. 开发语言

能够写CGI的开发语言太多,以下附上两个分别使用Perl和Python编写的同样功能的CGI, 正好能够做个对照。

这两个脚本能够接收Query String, 然后返回不同的文件。数据大小,以及缓存相关的头信息,能够区分二进制数据和文本数据。

CGI脚本支持以Query String改动以下响应头:

content type,

cache control,

content length (仅仅返回相应大小的数据),

last modified,

expires

以下是返回一个指定大小图片的样例:

/cgi/cache_factory.pl?type=image&size=32&last-modified=Fri, 02 Apr 2014 02:34:06 GMT&cache-control=private,max-age=60&expires=Fri, 22 Apr 2014 02:34:06 GMT

代码非常easy。能够做个參考。

Perl版本号

#!/usr/bin/perl
use strict;
use warnings;
use CGI; use constant BUFFER_SIZE => 4_096;
use constant DATA_DIRECTORY => "/var/www/Cache"; my $cgi = CGI->new();
my %params = $cgi->Vars(); &parserCommonHeaders; if(exists $params{'type'} && $params{'type'}=="image"){
&generateImageData;
}
else{
&generateTextData;
} sub parserCommonHeaders{
if(exists $params{'cache-control'}){
print 'Cache-Control:',$params{'cache-control'},"\r\n";
} if(exists $params{'last-modified'}){
print 'Last-Modified:',$params{'last-modified'},"\r\n";
} if(exists $params{'expires'}){
print 'Expires:',$params{'expires'},"\r\n";
} if(exists $params{'etag'}){
print 'ETag:ea6186e11526ce1:0',"\r\n";
}
} sub generateImageData{
my $buffer = ""; my $targetSize = 100*1024*1024;
if(exists $params{'size'} && $params{'size'}>0){
$targetSize = 1024*$params{'size'};
print "Content-length: $targetSize \r\n";
} my $image = DATA_DIRECTORY .'/images/very_big.jpg';
my( $type ) = $image =~ /\.(\w+)$/;
$type eq "jpg" and $type == "jpeg"; print $cgi->header( -type => "image/$type", -expires => "-1d" );
binmode STDOUT; local *IMAGE;
open IMAGE, $image or die "Cannot open file $image: $!"; my $sentSize = 0;
while ( read( IMAGE, $buffer, BUFFER_SIZE ) ) {
print $buffer;
$sentSize += BUFFER_SIZE; if($sentSize>=$targetSize){
last;
}
}
close IMAGE;
} sub generateTextData{
my $startHeader = '<html><head><title>HTTP Cache Testing HTML</title></head><body>';
my $tailPart = '</body></html>'; if(exists $params{'type'}){
print "Content-type:$params{'type'}\r\n";
}
else{
print "Content-type:text/html\r\n";
} my $targetTextSize = 100*1024*1024;
if(exists $params{'size'} && $params{'size'}>0){
$targetTextSize = 1024*$params{'size'};
print "Content-length: $targetTextSize \r\n";
}
print "\r\n"; $targetTextSize -= length($startHeader) + length($tailPart); print "$startHeader"; my $filepath = DATA_DIRECTORY .'/files/big_text.txt'; open(FILE, $filepath) or die $!;
my @lines = <FILE>;
close(FILE); foreach my $line (@lines) {
if( length($line)<=$targetTextSize ){
print $line;
}
else{
print substr($line,0,$targetTextSize);
} $targetTextSize -= length($line); if($targetTextSize<=0){
last;
}
} print "$tailPart";
}

Python版本号

#!/usr/bin/python
import os
import cgi
import sys BUFFER_SIZE = 4096
DATA_DIRECTORY = "/var/www/Cache" def parserCommonHeaders(formQuery):
if('cache-control' in formQuery.keys()):
print 'Cache-Control:',formQuery['cache-control'].value,"\r\n", if('last-modified' in formQuery.keys()):
print 'Last-Modified:',formQuery['last-modified'].value,"\r\n", if('expires' in formQuery.keys()):
print 'Expires:',formQuery['expires'].value,"\r\n", if('etag' in formQuery.keys()):
print 'ETag:ea6186e11526ce1:0',"\r\n", def generateImageData(formQuery):
targetSize = 100*1024*1024;
if('size' in formQuery.keys()):
targetSize = 1024*int(formQuery['size'].value)
print "Content-length:",targetSize,"\r\n", image = DATA_DIRECTORY+'/images/very_big.jpg'
print "Content-Type:image/jpeg\r\n", print sentSize = 0
f = open(image, 'rb')
while True:
data = f.read(4096)
sentSize = sentSize + BUFFER_SIZE
sys.stdout.write(data)
if sentSize>targetSize:
break sys.stdout.flush()
close(f) def generateTextData(formQuery):
startHeader = '<html><head><title>HTTP Cache Testing HTML</title></head><body>'
tailPart = '</body></html>' targetTextSize = 2.3*1024*1024;
if('size' in formQuery.keys()):
targetTextSize = 1024*int(formQuery['size'].value)
print "Content-length:",targetTextSize,"\r\n", if('type' in formQuery.keys()):
print "Content-type: %s\r\n"%(formQuery['type'].value),
else:
print "Content-type: text/html\r\n", print print startHeader targetTextSize = targetTextSize - len(startHeader) - len(tailPart) filepath = DATA_DIRECTORY + '/files/big_text.txt' file = open(filepath)
lines = file.readlines()
file.close() for line in lines:
if( len(line) <= targetTextSize ):
print line
else:
print line[0:targetTextSize] targetTextSize = targetTextSize - len(line) if(targetTextSize<=0):
break print tailPart if __name__ =="__main__":
formQuery = cgi.FieldStorage() #os.environ['QUERY_STRING'] parserCommonHeaders(formQuery) if('type' in formQuery.keys() and formQuery['type'].value=="image"):
generateImageData(formQuery)
else:
generateTextData(formQuery)

四. 服务器

服务器端使用Apache Server+WANem, 配合CGI完毕灵活的需求,开通SFTP端口供相关同学编辑。方便共享測试用例。

.-----.   .-------.

| CGI |   | WANem |

'-----'---'-------'

| Apache Server   |

'-----------------'

^

|

SFTP & HTTP

|

.------------------.

| Web Page Editor  |

| and Browser      |

'------------------'

配有WANem最大的优点就是能够依据需求进网络状态调整, 甚至能够用相似以下的方式在測试用例动态调整。

參考:

Apache配置 (不要忘记给CGI脚本加上可运行权限)

CGI Programming 101 (Perl)

Debugging CGI Programs (Perl)

Python CGI编程

Python CGI Debugging

Perl &amp; Python编写CGI的更多相关文章

  1. 关于CGI:Tomcat、PHP、Perl、Python和FastCGI之间的关系

    如前文所述,Web服务器是一个很简单的东西,并不负责动态网页的构建,只能转发静态网页.同时Apache也说,他能支持perl,生成动态网页.这个支持perl,其实是apache越位了,做了一件额外的事 ...

  2. 常用脚本语言Perl,Python,Ruby,Javascript一 Perl,Python,Ruby,Javascript

    常用脚本语言Perl,Python,Ruby,Javascript一 Perl,Python,Ruby,Javascript Javascript现阶段还不适合用来做独立开发,它的天下还是在web应用 ...

  3. 用Python编写一个简单的Http Server

    用Python编写一个简单的Http Server Python内置了支持HTTP协议的模块,我们可以用来开发单机版功能较少的Web服务器.Python支持该功能的实现模块是BaseFTTPServe ...

  4. 正则表达式匹配可以更快更简单 (but is slow in Java, Perl, PHP, Python, Ruby, ...)

    source: https://swtch.com/~rsc/regexp/regexp1.html translated by trav, travmymail@gmail.com 引言 下图是两种 ...

  5. Python编写简易木马程序(转载乌云)

    Python编写简易木马程序 light · 2015/01/26 10:07 0x00 准备 文章内容仅供学习研究.切勿用于非法用途! 这次我们使用Python编写一个具有键盘记录.截屏以及通信功能 ...

  6. 基于python编写的天气抓取程序

    以前一直使用中国天气网的天气预报组件都挺好,可是自从他们升级组件后数据加载变得非常不稳定,因为JS的阻塞常常导致网站打开速度很慢.为了解决这个问题决定现学现用python编写一个抓取程序,每天定时抓取 ...

  7. 用Python编写博客导出工具

    用Python编写博客导出工具 罗朝辉 (http://kesalin.github.io/) CC 许可,转载请注明出处   写在前面的话 我在 github 上用 octopress 搭建了个人博 ...

  8. 【转载】Python编写简易木马程序

    转载来自: http://drops.wooyun.org/papers/4751?utm_source=tuicool 使用Python编写一个具有键盘记录.截屏以及通信功能的简易木马. 首先准备好 ...

  9. 用Python编写的第一个回测程序

    用Python编写的第一个回测程序 2016-08-06 def savfig(figureObj, fn_prefix1='backtest8', fn_prefix2='_1_'): import ...

随机推荐

  1. LoadRunner去除事物中的程序的执行时间

    大家在性能测试过程中,经常会用到程序处理或组织数据,以达到一定的测试目的,但是程序本身执行会消耗一些时间,这部分消耗的时间是包含在响应时间里面,此时,响应时间=正常响应时间+程序执行消耗时间.那么如何 ...

  2. Android逆向之旅---反编译利器Apktool和Jadx源码分析以及错误纠正

    Android逆向之旅---反编译利器Apktool和Jadx源码分析以及错误纠正 http://blog.csdn.net/jiangwei0910410003/article/details/51 ...

  3. 【转】python+django+vue搭建前后端分离项目

    https://www.cnblogs.com/zhixi/p/9996832.html 以前一直是做基于PHP或JAVA的前后端分离开发,最近跟着python风搭建了一个基于django的前后端分享 ...

  4. 初识GeneXus产品

    本人由于工作原因接触了GeneXus产品,从使用到现在也有些年头了.从刚开始的不熟悉到现在使用GeneXus开发了很多项目,慢慢也总结了一些经验,当然中间也走了很多的弯路.对于在国内同样使用GeneX ...

  5. 快速配置webpack多入口脚手架

    背景 当我们基于vue开发单个项目时,我们会init一个vue-cli,但当我们想在其他项目里共用这套模板时,就需要重新init一个,或者clone过来,这非常不方便,而且当多人开发时,我们希望所有的 ...

  6. Linux-数据库2

    表记录的操作 增 1.插入一条记录 语法:insert [into] tab_name (field1,filed2,.......) values (value1,value2,.......); ...

  7. (bc 1001) hdu 6015 skip the class

    Skip the Class Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) T ...

  8. 【BZOJ 3144】 3144: [Hnoi2013]切糕 (最小割模型)

    3144: [Hnoi2013]切糕 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1764  Solved: 965 Description Inp ...

  9. 【UOJ 34】 多项式乘法 (FFT)

    [题意] 给你两个多项式,请输出乘起来后的多项式. 先打一个递归版本的模板... #include<cstdio> #include<iostream> #include< ...

  10. java switch

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha switch 是 开关:转换 的意思. 支持的数据类型 有 : 字节,字符,短整型,整型 ...