【问题标题】:urllib2 urlopen error: must be string or buffer, not instanceurllib2 urlopen 错误:必须是字符串或缓冲区,而不是实例
【发布时间】:2015-07-16 18:43:20
【问题描述】:

我有一个简单的 Python 脚本,如下所示,它可以访问 PHP 脚本。

import urllib2
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
url="http://192.168.122.1/info.php/"
datagen, headers = multipart_encode({"file": open("123.txt", 'r')})
request = urllib2.Request(url,datagen,headers)
response = urllib2.urlopen(request)
data = response.read();
print data

但我收到以下错误:

Traceback (most recent call last):
  File "sample.py", line 8, in <module>
    response = urllib2.urlopen(request)
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 449, in _open
'_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1227, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1194, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "/usr/lib/python2.7/httplib.py", line 1048, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.7/httplib.py", line 1088, in _send_request
    self.endheaders(body)
  File "/usr/lib/python2.7/httplib.py", line 1044, in endheaders
    self._send_output(message_body)
  File "/usr/lib/python2.7/httplib.py", line 892, in _send_output
    self.send(message_body)
  File "/usr/lib/python2.7/httplib.py", line 864, in send
    self.sock.sendall(data)
  File "/usr/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
TypeError: must be string or buffer, not instance

这可能是什么原因?还是我的 PHP 脚本有问题。

这是我的 PHP 脚本

<?php
        echo "Hello";
?>

【问题讨论】:

  • 你想要那个网站的回应
  • @VigneshKalai 是的。我只是返回一个字符串..

标签: python urllib2


【解决方案1】:

我发现了错误。

https://stackoverflow.com/a/27052062/3460885

从这个问题开始,我不得不在开头添加 register_openers() 。

【讨论】:

  • 将您自己的答案标记为已接受。 ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-07-14
  • 2019-02-17
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
相关资源
最近更新 更多