我想你不明白post '/upload' 和uploadtest.pl 是什么。
您的脚本是一个小型 HTTP 服务器,它接收请求并做出响应。
您可以永久运行您的服务器并使用例如curl 发出请求,或者对其发出一个请求并退出。
$ perl uploadtest.pl daemon
[Sat Jun 11 14:00:41 2016] [info] Listening at "http://*:3000"
Server available at http://127.0.0.1:3000
# in other shell
$ curl -X POST "http://localhost:3000/uploadtest.pl/status?Token=6d949625-2f07-1e7a-d57a-de0fa949035d"
或两者兼而有之:
$perl ./uploadtest.pl get -v -M POST /status?"Token=3780e99a-fc2c-54e5-2c31-417f945c1792"
这些 POST 向具有空内容
的永久(第一个示例)或一次性服务器(第二个)发出请求
如果您使用 -v 选项运行 curl:
$ curl -v -X POST "http://localhost:3000/status?Token=6d949625-2f07-1e7a-d57a-de0fa949035d"
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST /status?Token=6d949625-2f07-1e7a-d57a-de0fa949035d HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:3000
> Accept: */*
>
< HTTP/1.1 200 OK
* Server Mojolicious (Perl) is not blacklisted
< Server: Mojolicious (Perl)
< Date: Sat, 11 Jun 2016 11:42:24 GMT
< Content-Length: 48
< Content-Type: text/html;charset=UTF-8
<
* Connection #0 to host localhost left intact
In process: 6d949625-2f07-1e7a-d57a-de0fa949035d
您甚至可以在查询服务器时使用tcpdump:
$ sudo tcpdump -x -X -i lo port 3000
14:41:51.665032 IP localhost.49063 > localhost.3000: Flags [P.], seq 1:129, ack 1, win 342, options [nop,nop,TS val 1097613 ecr 1097613], length 128
0x0000: 4500 00b4 38c4 4000 4006 037e 7f00 0001 E...8.@.@..~....
0x0010: 7f00 0001 bfa7 0bb8 3b17 8750 a281 4839 ........;..P..H9
0x0020: 8018 0156 fea8 0000 0101 080a 0010 bf8d ...V............
0x0030: 0010 bf8d 504f 5354 202f 7374 6174 7573 ....POST./status
0x0040: 3f54 6f6b 656e 3d36 6439 3439 3632 352d ?Token=6d949625-
0x0050: 3266 3037 2d31 6537 612d 6435 3761 2d64 2f07-1e7a-d57a-d
0x0060: 6530 6661 3934 3930 3335 6420 4854 5450 e0fa949035d.HTTP
0x0070: 2f31 2e31 0d0a 5573 6572 2d41 6765 6e74 /1.1..User-Agent
0x0080: 3a20 6375 726c 2f37 2e33 352e 300d 0a48 :.curl/7.35.0..H
0x0090: 6f73 743a 206c 6f63 616c 686f 7374 3a33 ost:.localhost:3
0x00a0: 3030 300d 0a41 6363 6570 743a 202a 2f2a 000..Accept:.*/*
0x00b0: 0d0a 0d0a ....
当你想上传文件到你的服务器时,你使用-F Input_File=@d:/xml/test.xml curl 选项。您还希望您可以执行相同的 onetime 请求,但下一个命令:
$perl ./uploadtest.pl get -v -M POST /upload?"Input_Type=XML&Input_File=d:/xml/test.xml"
不支持此类选项。
$perl ./uploadtest.pl help get
Usage: APPLICATION get [OPTIONS] URL [SELECTOR|JSON-POINTER] [COMMANDS]
./myapp.pl get /
./myapp.pl get -H 'Accept: text/html' /hello.html 'head > title' text
./myapp.pl get //sri:secr3t@/secrets.json /1/content
mojo get mojolicious.org
mojo get -v -r google.com
mojo get -v -H 'Host: mojolicious.org' -H 'Accept: */*' mojolicious.org
mojo get -M POST -H 'Content-Type: text/trololo' -c 'trololo' perl.org
mojo get mojolicious.org 'head > title' text
mojo get mojolicious.org .footer all
mojo get mojolicious.org a attr href
mojo get mojolicious.org '*' attr id
mojo get mojolicious.org 'h1, h2, h3' 3 text
mojo get https://api.metacpan.org/v0/author/SRI /name
Options:
-C, --charset <charset> Charset of HTML/XML content, defaults to
auto-detection
-c, --content <content> Content to send with request
-H, --header <name:value> Additional HTTP header
-h, --help Show this summary of available options
--home <path> Path to home directory of your application,
defaults to the value of MOJO_HOME or
auto-detection
-M, --method <method> HTTP method to use, defaults to "GET"
-m, --mode <name> Operating mode for your application, defaults to
the value of MOJO_MODE/PLACK_ENV or
"development"
-r, --redirect Follow up to 10 redirects
-v, --verbose Print request and response headers to STDERR
从帮助中可以看到,可以发送短信:
./uploadtest.pl get -M POST -H 'Content-Type: text/trololo' -c 'trololo' perl.org
要将文件上传到您的uploadtest.pl 服务器,您可以编写自己的上传器。为此,您应该使用Mojo::UserAgent 模块。 Example
use Mojo::UserAgent;
# Upload file via POST and "multipart/form-data"
my $ua = Mojo::UserAgent->new;
$ua->post("http://localhost:3000" => form => {Input_Type => "XML", Input_File => {file => "d:/xml/test.xml"}});
但这是same:
perl -Mojo -E 'p("http://localhost:3000" => form => {Input_Type => "XML", Input_File => {file => "d:/xml/test.xml"}})'
UPD
要调试用于文件上传的服务器端脚本,您应该
perl -d uploadtest.pl daemon
Loading DB routines from perl5db.pl version 1.49_04
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
main::(tx.pl:6): get '/' => 'form';
DB<1>
这将带你进入perl debugger (Tutorial)
DB<1> l 1-30
1 #uploadtest.pl
2
3: use Mojolicious::Lite;
4
5 # Upload form in DATA section
6==> get '/' => 'form';
7
8
9 # Check status
10 post '/status' => sub {
11: my $self = shift;
12: my $Token = $self->param('Token');
13: $self->render(text => "In process: $Token");
14: };
15
16 # Multipart upload handler
17 post '/upload' => sub {
18: my $self = shift;
19
20 # Process uploaded file
21: my $Input_File = $self->param('Input_File');
22: my $Input_Type = $self->param('Input_Type');
23: my $size = $Input_File->size;
24: my $name = $Input_File->filename;
25: my $upload = $self->req->upload('Input_File');
26: $upload->move_to("d:/xml/$name"); #move location
27: $self->render(text => "Thanks for uploading $size byte file $name.");
28: };
29
30: app->start;
DB<2> b 11
DB<3> b 18
DB<4> l 1-30
1 #uploadtest.pl
2
3: use Mojolicious::Lite;
4
5 # Upload form in DATA section
6==> get '/' => 'form';
7
8
9 # Check status
10 post '/status' => sub {
11:b my $self = shift;
12: my $Token = $self->param('Token');
13: $self->render(text => "In process: $Token");
14: };
15
16 # Multipart upload handler
17 post '/upload' => sub {
18:b my $self = shift;
19
20 # Process uploaded file
21: my $Input_File = $self->param('Input_File');
22: my $Input_Type = $self->param('Input_Type');
23: my $size = $Input_File->size;
24: my $name = $Input_File->filename;
25: my $upload = $self->req->upload('Input_File');
26: $upload->move_to("d:/xml/$name"); #move location
27: $self->render(text => "Thanks for uploading $size byte file $name.");
28: };
29
30: app->start;
DB<5> c
[Tue Jun 14 16:43:06 2016] [info] Listening at "http://*:3000"
Server available at http://127.0.0.1:3000
[Tue Jun 14 16:43:10 2016] [debug] POST "/status"
[Tue Jun 14 16:43:10 2016] [debug] Routing to a callback
main::CODE(0x23f7be0)(tx.pl:11): my $self = shift;
DB<5> n
main::CODE(0x23f7be0)(tx.pl:12): my $Token = $self->param('Token');
DB<5> n
main::CODE(0x23f7be0)(tx.pl:13): $self->render(text => "In process: $Token");
DB<5> x $Token
0 '6d949625-2f07-1e7a-d57a-de0fa949035d'
DB<6>
这里我列出了1到30行的源代码l 1-30,设置了两个断点b 11b 18,再次列出源代码l 1-30,看看断点是否生效(注意b在附近行),从当前点运行脚本(注意行附近的 ==> 符号)直到下一个断点,使用n 命令完成两个步骤并查看 $Token 的值em> 带有x $Token 命令的变量。
注意,c 脚本等待交互之后。我用curl 来做到这一点
http://localhost:3000/status?Token=6d949625-2f07-1e7a-d57a-de0fa949035d
注意:您应该像以前一样请求“/status”和“/upload”不“/updater.pl/status”。您的服务器脚本如何命名与外部单词无关。只有两件事:
- 你的脚本在哪里监听:localhost:3000
- 可以服务的请求:“/upload”和“/status”