urllib.request在urlopen

时间:2019-02-16 16:11:29

标签: python-3.x urllib python-3.7

我写了一段代码,可以将值从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);
?>

0 个答案:

没有答案