2

我对 perl 一无所知,但是从一个大的 perl 脚本中,我设法获取了相关部分并发出了 HTTP 请求。因此,这个 perl 代码可以完美运行。

#!/usr/bin/perl -w

use strict;
use LWP::UserAgent;
use HTTP::Request::Common;

my $ua = new LWP::UserAgent;

my $request = "X-CTCH-PVer: 0000001\r\n";

my $method_url =  "http://localhost:8088/ctasd/GetStatus";
my $response = $ua->request (POST $method_url,Content => $request);

my $data = $response->status_line . "\n";
print $data;
print $response->content;

上面的代码输出:

200 OK
X-CTCH-PVer: 0000001

据我了解,它正在对具有指定数据的 URL 进行 POST。有了这个基础,我的 python 代码看起来像:

#!/usr/bin/python

import urllib

url = "http://localhost:8088/ctasd/GetStatus"
data = urllib.urlencode([("X-CTCH-PVer", "0000001")])

print urllib.urlopen(url, data).read()

但是,这将返回响应为:

X-CTCH-Error: Missing protocol header "X-CTCH-PVer"

请帮助我制作与 perl 代码等效的 Python。

4

2 回答 2

2

所以,实际情况是,$requestPerl 中的 POST 数据实际上是作为 POST 数据发送的,没有任何更改。现在我明白了为什么contentPerl 中的名称是这样的。

#!/usr/bin/python

import urllib

url = "http://localhost:8088/ctasd/GetStatus"
print urllib.urlopen(url, "X-CTCH-PVer: 0000001").read()

工作。在两种情况下捕获流量并在wireshark中分析后,我实际上发现了这一点。

于 2012-12-26T08:08:59.253 回答
0

错误是因为您没有发送标头,您正在制作/发送 urlencoded 字符串,因此该函数urllib.urlencode

尝试使用实际标头设置请求:

#!/usr/bin/python

import urllib2


request = urllib2.Request("http://localhost:8088/ctasd/GetStatus", headers={"X-CTCH-PVer" : "0000001"})
contents = urllib2.urlopen(request).read()
于 2012-12-26T07:09:37.440 回答