Python httplib2, AttributeError: set object has no attribute items

2024/10/2 14:26:32

I'm playing with the Python library httplib2. The following is my code.

import urllib.parse
import httplib2httplib2.debuglevel = 1http = httplib2.Http()url = "http://login.sina.com.cn/hd/signin.php"
body = {"act": "1","entry": "vblog","password": "P@$sW0rd","reference": "http://vupload.you.video.sina.com.cn/u.php?m=1&cate=0","reg_entry": "vblog","remLoginName": "on","username": "this_is_user_name","x": "","y": ""}headers = {"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8","Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7","Accept-Encoding", "gzip,deflate","Accept-Language", "en-us,en;q=0.5","Connection", "keep-alive","Content-Length", "181","Content-Type", "application/x-www-form-urlencoded","Host", "login.sina.com.cn","Keep-Alive", "115","Referer", "http://login.sina.com.cn/hd/signin.php?entry=vblog&r=http%3A%2F%2Fvupload.you.video.sina.com.cn%2Fu.php%3Fm%3D1%26cate%3D0","User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16"}response, content = http.request(url, 'POST', headers=headers, body=urllib.parse.urlencode(body))

When I execute it, I get the error:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.C:\>python --version
Python 3.2C:\>python a.py
Traceback (most recent call last):File "a.py", line 32, in <module>response, content = http.request(url, 'POST', urllib.parse.urlencode(body), headers=headers)File "C:\Python32\lib\site-packages\httplib2\__init__.py", line 961, in requestheaders = _normalize_headers(headers)File "C:\Python32\lib\site-packages\httplib2\__init__.py", line 184, in _normalize_headersreturn dict([ (key.lower(), NORMALIZE_SPACE.sub(value, ' ').strip())  for (key, value) in headers.items()])
AttributeError: 'set' object has no attribute 'items'C:\>

I'm pretty new to Python and httplib2, anything wrong in my code?

Answer

headers should be a dictionary, not a set:

headers = {"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",...}

Note the colon instead of the comma.

https://en.xdnf.cn/q/70841.html

Related Q&A

Atom IDE autocomplete-python not working

I have just installed the Atom IDE and the package autocomplete-python (on Windows). But the package is not working. Do I have to make any setting changes? (I have disabled autocomplete-plus and autoc…

Multiple instances of a class being overwritten at the same time? (Python)

Heres a very simple code I made to demonstrate the problem Im encountering. Whats happening here is that Im creating two different instances of the same class but changing an attribute of one will cha…

how to store binary file recieved by Flask into postgres

I currently have a Flask route that reveives file content via POST, and that stores it on the file system, ex: @app.route(/upload, methods=[POST]) def upload_file():def allowed_file(f):return Truefile …

How can I kill a single shot QtCore.QTimer in PyQt4?

So, in my application, I create a single QtCore.QTimer object and then call the singleShot method on it to evoke a function after say 60 secs. Now, at any given point in time, if I would need to call t…

How to convert list of lists to a set in python so I can compare to other sets?

I have a list users_with_invites_ids_list, formed by loop where I append values to the list, in python that looks like this:...[ObjectId(55119e14bf2e4e010d8b48f2)], [ObjectId(54624128bf2e4e5e558b5a52)]…

How can I create an ODBC connection to SAS?

Im writing a program that needs to access SAS data. Ive downloaded the ODBC drivers for SAS and installed them, but I need to be able to create ODBC connections on the fly, programmatically. The foll…

How to extract links from a page using Beautiful soup

I have a HTML Page with multiple divs like:<div class="post-info-wrap"><h2 class="post-title"><a href="https://www.example.com/blog/111/this-is-1st-post/" t…

Verifying the integrity of PyPI Python packages

Recently there came some news about some Malicious Libraries that were uploaded into Python Package Index (PyPI), see:Malicious libraries on PyPI Malicious modules found into official Python repository…

How to get results from custom loss function in Keras?

I want to implement a custom loss function in Python and It should work like this pseudocode:aux = | Real - Prediction | / Prediction errors = [] if aux <= 0.1:errors.append(0) elif aux > 0.1 &am…

How to tell whether a file is executable on Windows in Python?

Im writing grepath utility that finds executables in %PATH% that match a pattern. I need to define whether given filename in the path is executable (emphasis is on command line scripts).Based on "…