php - 从 Python 发送 HTTP POST 请求(尝试从 PHP 转换)

标签 php python http-headers porting

我正在尝试将此代码片段从 PHP 转换为 Python(编程新手),但我发现这样做很困难:

我尝试转换的 PHP 如下:

$fp = fsockopen($whmcsurl, 80, $errno, $errstr, 5);
if ($fp) {
$querystring = "";
foreach ($postfields AS $k=>$v) {
$querystring .= "$k=".urlencode($v)."&";
}
$header="POST ".$whmcsurl."modules/servers/licensing/verify.php HTTP/1.0\r\n";
$header.="Host: ".$whmcsurl."\r\n";
$header.="Content-type: application/x-www-form-urlencoded\r\n";
$header.="Content-length: ".@strlen($querystring)."\r\n";
$header.="Connection: close\r\n\r\n";
$header.=$querystring;
$data="";
@stream_set_timeout($fp, 20);
@fputs($fp, $header);
$status = @socket_get_status($fp);
while (!@feof($fp)&&$status) {
$data .= @fgets($fp, 1024);
$status = @socket_get_status($fp);
}
@fclose ($fp);
}

我写的对应的Python代码如下:

fp = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
                fp.connect(("my ip", 80))
                if (fp):
                        querystring = ""
                        #print postfields
                        for key in postfields:
                                querystring = querystring+key+"="+urllib.quote(str(postfields[key]))+"&"
                header = "POST "+whmcsurl+"modules/servers/licensing/verify.php HTTP/1.0\r\n"
                header+="Content-type: application/x-www-form-urlencoded\r\n"
                header+="Content-length: "+str(len(querystring))+"\r\n"
                header+="Connection: close\r\n\r\n"
                #header+=querystring
                data=""
                request = urllib2.Request(whmcsurl,querystring,header)
                response = urllib2.urlopen(request)
                data = response.read()

在这里,我遇到了以下错误:

request = urllib2.Request(whmcsurl,querystring,header)
  File "/usr/lib64/python2.6/urllib2.py", line 200, in __init__
    for key, value in headers.items():
AttributeError: 'str' object has no attribute 'items'

所以我猜测 Python 需要一个用于标题的字典。但 PHP 将其作为字符串发送。

我可以知道如何解决这个问题吗?

提前致谢

最佳答案

你把事情复杂化了相当长的一段距离。 Python 会为您处理大部分工作。无需自己打开套接字,也无需构建 header 和 HTTP 开头行。

使用 urllib.requesturllib.parse为您完成工作的模块:

from urllib.parse import urlopen
from urllib.request import urlopen

params = urlencode(postfields)
url = whmcsurl + 'modules/servers/licensing/verify.php'
response = urlopen(url, params)
data = response.read()

urlopen() 接受第二个参数,即要在 POST 请求中发送的数据;该库负责计算正文的长度,并设置适当的标题。最重要的是,它在幕后使用了另一个库 httplib , 以处理套接字连接并生成有效的 header 和 HTTP 请求行。

POST 正文是使用 urllib.parse.urlencode() 编码的,它还负责为您正确引用。

您可能还想查看 external requests library ,它仍然提供了一个更易于使用的 API:

import requests

response = requests.post(whmcsurl + 'modules/servers/licensing/verify.php', params=params)
data = response.content  # or response.text for decoded content, or response.json(), etc.

关于php - 从 Python 发送 HTTP POST 请求(尝试从 PHP 转换),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18074616/

相关文章:

javascript - 如何使用 Javascript 设置授权 HTTP header

google-chrome - 什么是 delegate_MojoAsyncResourceHandler

php - Paypal 付款拆分为 2 个合作伙伴

php - 记住不使用 Laravel 的用户

php - 想要使 http ://test. domain.tld/go 到 http ://domain. tld/test

python - 类中的嵌套字典

php - 如何在表单中使用 FormHelper::postLink()?

python - 使用 Python lfilter 过滤信号

Python - 在 return 内嵌套 if else

jsf-2 - 如何删除(重复)X-Powered-By : JSF/2. 0