设置 HTTP 请求记录器的最简单方法是什么

设置 HTTP 请求记录器的最简单方法是什么

在本地设置某些内容以侦听自定义端口上的 GET 请求的最简单方法是什么?

这样我就可以做curl -X GET -i 'localhost:34331/hello'

验证是否收到请求并检查请求以查看标头(如果发送了标头)以及用于请求的 URL

答案1

您可以使用netcat。就像是:

nc -l 34331

-v如果你想查看调用的详细信息,使用curl的选项并调用真实的服务会更容易。

curl -v http://www.google.com > /dev/null
* Connected to localhost (127.0.0.1) port 3128 (#0)
> GET http://www.google.com/ HTTP/1.1
> Host: www.google.com
> User-Agent: curl/7.47.0
> Accept: */*
> Proxy-Connection: Keep-Alive
> 
< HTTP/1.1 200 OK
< Date: Fri, 22 Jun 2018 12:58:58 GMT
< Expires: -1
< Cache-Control: private, max-age=0
< P3P: CP="This is not a P3P policy! See g.co/p3phelp for more info."
< Server: gws
< X-XSS-Protection: 1; mode=block
< X-Frame-Options: SAMEORIGIN
< Set-Cookie: 1P_JAR=2018-06-22-12; expires=Sun, 22-Jul-2018 12:58:58 GMT; path=/; domain=.google.com
< Set-Cookie: NID=132=cLF8pa3SHRsg32-ZGzN5aZ3ipLfAbxqfmUvJ2NTvkYg2eWN6XaOqSofMK7o902-C9hdxL_wUn6cJW2AkngcQXvNUKCCdNi7Z-eBTu0Yc8-iTFR90OeZDR44hxZK95_Ny; expires=Sat, 22-Dec-2018 12:58:58 GMT; path=/; domain=.google.com; HttpOnly
< Accept-Ranges: none
< Vary: Accept-Encoding
< Transfer-Encoding: chunked
< Content-Type: text/html; charset=ISO-8859-1
< Connection: keep-alive
< Proxy-Connection: keep-alive
< 
{ [2035 bytes data]
100 11564    0 11564    0     0  69134      0 --:--:-- --:--:-- --:--:-- 68833
* Connection #0 to host localhost left intact

答案2

如果您安装了 Perl,则可以使用 HTTP::Daemon 模块(您需要在 Perl 库中拥有该模块)编写一个非常简单的 http 守护进程:

#!/usr/bin/perl
use strict;
use HTTP::Daemon;
use HTTP::Response;
use threads;

my $httpd = HTTP::Daemon->new( LocalPort => 8081, Listen => 20, Reuse=>1) || die;
print "httpd started...\n";

sub process_one_req
{
  my $con = shift;
  my $rq = $con->get_request;

  print "<< ".$rq->uri." : ".$rq->header('User-Agent')."\n";

  my $rsp = HTTP::Response->new(200, 'OK' );
  $con->send_response($rsp);
} 

while (my $con = $httpd->accept)
{
 process_one_req $con;
}

也可以看看https://metacpan.org/pod/HTTP::Daemon

相关内容