pycurl error. TypeError: invalid arguments to setopt

0

I made this code with the pycurl library

import io
import pycurl
from StringIO import StringIO    

c = pycurl.Curl()
c.setopt(pycurl.URL, "http://www.twitter.com/")
c.setopt(pycurl.HTTPHEADER, ["Accept:"])
c.setopt(pycurl.COOKIEFILE, "cookieDeLogueoTwitter.txt")
c.setopt(pycurl.REFERER,"http://www.twitter.com/")
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(pycurl.SSL_VERIFYHOST, 0)

e = io.BytesIO()
c.setopt(pycurl.WRITEFUNCTION, e.write)

c.perform()
print c.getinfo(pycurl.HTTP_CODE), c.getinfo(pycurl.EFFECTIVE_URL)

content = e.getvalue()

posicion_token = content.find("authenticity_token")

print "El token es"

token = content[posicion_token-48:posicion_token-8]

##
print content[posicion_token-48:posicion_token-8]
##

token = str(token)

print token

c.setopt(pycurl.URL, "https://twitter.com/sessions")
c.setopt(pycurl.HTTPHEADER, ["Accept:"])
c.setopt(pycurl.COOKIEFILE, "cookieDeLogueoTwitter.txt")
c.setopt(pycurl.REFERER,"http://www.twitter.com/")
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.SSL_VERIFYPEER, 0)
c.setopt(pycurl.SSL_VERIFYHOST, 0)
c.setopt(pycurl.POST, 1)

username = "user"
password = "password"

postfields = { "session[username_or_email]" : "usuario" , "session[password]" : "password","return_to_ssl": "true", "scribe_log": "", "redirect_after_login":"%2F", "authenticity_token": token }

print postfields

c.setopt(pycurl.POSTFIELDS, postfields)

and he throws me the following error

  

Traceback (most recent call last): File   "C: \ Users \ User \ Desktop \ instabot-twitbot \ logueo_twitter2.py", line   56, in       c.setopt (pycurl.POSTFIELDS, postfields) TypeError: invalid arguments to setopt

But supposedly the argument POSTFIELDS exists according to the examples that there are

Pycurl postfields example

What could be the error then?

    
asked by Pablo 28.12.2017 в 13:18
source

1 answer

1

What happens is that you are directly sending a dictionary in postfields when you should pass a string. What you could eventually do is:

import urllib

postfields = urllib.urlencode(postfields)
c.setopt(pycurl.POSTFIELDS, postfields)
print postfields

> return_to_ssl=true&session%5Bpassword%5D=password&authenticity_token=111&redirect_after_login=%252F&scribe_log=&session%5Busername_or_email%5D=usuario

Through urllib.encode() we can map a sequence of tuples (in your case the dictionary postfields ) to a valid string for a POST request.

    
answered by 29.12.2017 / 04:07
source