我写了一段代码,可以将值从python3发送到php,但出现类似
的错误Python\Python37-32\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
Python\Python37-32\lib\urllib\request.py", line 523, in open
req = meth(req)
Python\Python37-32\lib\urllib\request.py", line 1247, in do_request_
raise TypeError(msg)
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.
import urllib
import urllib.request
val=[('a','1'),('b','2')]
val=urllib.parse.urlencode(val)
url='http://localhost:8080/new.php'
req=urllib.request.Request(url, val)
req.add_header("Content-type", "application/x-www-form-urlencoded")
page = urllib.request.urlopen(req)
p1=page.read()
这应该将a和b的值发送到new.php 邮编
<?php
$data["name"]= $_POST['one'];
$data["nme"]= $_POST['two'];
file_put_contents("filename.txt", $data);
?>