urllib TypeError: memoryview: dict object does not have the buffer interface

import urllib.request
url = ‘site‘
headers = {‘Authorization‘ : ‘Basic emVkMHg6WWJyYm5mMDA=‘}
req = urllib.request.Request(url, headers)
response = urllib.request.urlopen(req).getcode()

出现错误:

Traceback (most recent call last):
  File "C:\Program Files\Python33\lib\urllib\request.py", line 1186, in do_request_
    mv = memoryview(data)
TypeError: memoryview: dict object does not have the buffer interface

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/zed/Desktop/sddf.py", line 6, in <module>
    response = urllib.request.urlopen(req).getcode()
  File "C:\Program Files\Python33\lib\urllib\request.py", line 156, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Program Files\Python33\lib\urllib\request.py", line 467, in open
    req = meth(req)
  File "C:\Program Files\Python33\lib\urllib\request.py", line 1191, in do_request_
    data))
ValueError: Content-Length should be specified for iterable data of type <class ‘dict‘> {‘Authorization‘: ‘Basic emVkMHg6WWJyyM5mMDA=‘}

解决办法:

先看Requset定义:

class Request:
    def __init__(self, url, data=None, headers={},
                 origin_req_host=None, unverifiable=False,
                 method=None):

所以,应该改为

req = urllib.request.Request(url, headers = headers)
时间: 2024-08-04 13:48:56

urllib TypeError: memoryview: dict object does not have the buffer interface的相关文章

TypeError:&#39;dict&#39; object is not callable

TypeError:'dict' object is not callable 出现这种错误有两种可能: 1. 代码里重新定义了dict,比如 dict= {...},这时调用的是代码里定义的dict而不是python内置类型 2. 取字典内容时用了()而不是[].比如sdict("content_id"),应该是sdict["content_id"] TypeError:'dict' object is not callable

Python: TypeError: &#39;dict&#39; object is not callable

问题:  TypeError: 'dict' object is not callable 原因:  dict()是python的一个内建函数,如果将dict自定义为一个python字典,在之后想调用dict()函数是会报出"TypeError: 'dict' object is not callable"的错误, 解决办法:  >>>del (dict) Python: TypeError: 'dict' object is not callable 原文地址:ht

python问题:TypeError: a bytes-like object is required, not &#39;str&#39;

源程序: import socket target_host = "www.baidu.com" # 127.0.0.1 target_port = 80 # 建立一个socket对象 client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # 创建TCP连接 # 连接客户端 client.connect((target_host,target_port)) client.send("GET / HTTP/1.1\r

python 3.5: TypeError: a bytes-like object is required, not &#39;str&#39;

出现该错误往往是通过open()函数打开文本文件时,使用了'rb'属性,如:fileHandle=open(filename,'rb'),则此时是通过二进制方式打开文件的,所以在后面处理时如果使用了str()函数,就会出现该错误,该错误不会再python2中出现. 具体解决方法有以下两种: 第一种,在open()函数中使用'r'属性,即文本方式读取,而不是'rb',以二进制文件方式读取,可以直接解决问题. 第二种,在open()函数中使用'rb',可以在使用之前进行转换,有以下实例,来自:htt

&#39;dict&#39; object has no attribute &#39;a&#39;

a = {} #a.a = 'a' #AttributeError: 'dict' object has no attribute 'a' #a['a'] #KeyError: 'a' a['a'] = 'a' a #{'a': 'a'} 'dict' object has no attribute 'a'

Python 3.5.2 TypeError: a bytes-like object is required, not &#39;str’问题解决方案

运行环境Mac  Python 3.5.2 Q: http_response = """\ HTTP/1.1 200 OK Hello, World! """ client_connection.sendall(http_response) TypeError: a bytes-like object is required, not 'str' 类型错误,需要的是一个byte类型,不是str类型 A: http_response = "

AttributeError: &#39;dict&#39; object has no attribute &#39;has_key&#39;

后来发现时Python版本太新的原因!Python3以后删除了has_key()方法! 解决办法: 1.重新安装个Python,推荐2.7.6,用的人多些.好多人不习惯用3,仍然在用2 2.修改代码 if not rmap.has_key(cls): 改为 if cls not in rmap: AttributeError: 'dict' object has no attribute 'has_key'

TypeError: a bytes-like object is required, not &#39;str&#39;

python bytes和str两种类型转换的函数encode(),decode() str通过encode()方法可以编码为指定的bytes 反过来,如果我们从网络或磁盘上读取了字节流,那么读到的数据就是bytes.要把bytes变为str,就需要用decode()方法 例如: date="12345"  #str类型 date.encode()  #转换为bytes类型 TypeError: a bytes-like object is required, not 'str' 原文

Socket send() method throws TypeError: a bytes-like object is required, not &#39;str&#39;

python3 socket编程,发送data数据,会遇到需要bytes类型,而不是str字符串的错误 例如: import socket mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = socket.gethostbyname("www.google.com") mysock.connect(host, 80) message = "GET / HTTP/1.1\r\n\r\n"