#!/usr/bin/perl
#==============
# Date: 2012-11-03
# User: wcdj
#==============
use Socket;
use Carp;
use FileHandle;
# [1] use port 8080 by default, unless overridden on command line
$port = (@ARGV ? $ARGV[0] : 8080);
# [2] create local TCP socket and set it to listen for connections
$proto = getprotobyname('tcp');
socket(S, PF_INET, SOCK_STREAM, $proto) || die;
setsockopt(S, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die;
bind(S, sockaddr_in($port, INADDR_ANY)) || die;
listen(S, SOMAXCONN) || die;
# [3] print a startup message
printf("<<< perl_web_svr accepting on port %d >>>\n\n", $port);
while (1)
{
# [4] wait for a connection C(Client)
$cport_caddr = accept(C, S);
($cport, $caddr) = sockaddr_in($cport_caddr);
C->autoflush(1);
# [5] print who the connection is from
$cname = gethostbyaddr($caddr, AF_INET);
printf("<<< request from [%s] >>>\n", $cname);
# [6] read request msg until blank line, and print on screen
while ($line = <C>)
{
print $line;
if ($line =~ /^\r/) {last; }
}
# [7] prompt for response msg, and input response lines,
# sending response lines to client, until solitary "."
printf("<<< type response followed by '.' >>>\n");
while ($line = <STDIN>)
{
$line =~ s/\r//;
$line =~ s/\n//;
if ($line =~ /^\./) {last; }
print C $line . "\r\n";
}
close(C);
}
cgi perl 脚本
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 这段时间简书因为一些不可描述的文章被整顿了,所以没有做学习笔记。加上最近开学,乱七八糟的事情很多,目测要下...
- 脚本技能至关重要,是提升到黑客集团的高层。没有脚本技能,您依赖其他人来开发您的工具。当其他人开发您的工具时,您将始...