如何在json中接受http以及如何发送

时间:2019-05-06 13:25:23

标签: perl

我想编写一个将json格式的http请求发送到的api。我将能够捕获它们并将其存储在文件中。 我已经开始编写一个套接字代码,在该代码中它接受来自一个客户端的连接(不分叉),并且当我通过某个端口发送http时,我会收到http标头。 我只是不知道如何进行,因为我缺乏概念。我对perl相当陌生。 谢谢

use strict;
use warnings;
use IO::Socket;

my ($buf,$client_address,$client_port);
my $sock = new IO::Socket::INET
(
LocalHost => '0.0.0.0',
LocalPort => 5000,
Proto => 'tcp',
Listen => 5,
Reuse => 1
);
die "Socket could not be created. Reason: $!" unless $sock;
while (my $new_sock = $sock->accept())
 {
 $client_address = $new_sock->peerhost();
 $client_port = $new_sock->peerport();
      while (defined ($buf = <$new_sock>))
        {
            print $buf;
            log_file();
         }
 }
close ($sock);
sub log_file
{
my $time = localtime(time);
my $file = 'c:\temp\log.txt';
open(my $fh, '>>', $file) or die "Could not open file '$file' $!";print 
$fh "$time  connection received from $client_address:$client_port". 
"$buf\n";
print $fh $buf;
close $fh;
 }

0 个答案:

没有答案