I got this snippet out of my text book but it's giving me a type error, I'll just give you the whole code, tell me if you get it too, if you would.

import urllib.request
import math
whips=int(0)
rate=float(4)
while rate <= float(20):
    improve= rate*float(.15)+rate
    rate= improve
    whips=whips+1
print(whips)
def send_to_twitter():
    msg = whips
    password_manager = urllib.request.HTTPPasswordMgr()
    password_manager.add_password("Twitter API","http://twitter.com/statuses", "jcbamm26", "~~~~~~~")
    http_handler = urllib.request.HTTPBasicAuthHandler(password_manager)
    page_opener = urllib.request.build_opener(http_handler)
    urllib.request.install_opener(page_opener)
    params = urllib.parse.urlencode( {'status': msg} )
    resp = urllib.request.urlopen('http://twitter.com/statuses/update.json', params)
    resp.read()
send_to_twitter()

the error return is

Traceback (most recent call last):
  File "C:\Python32\AB3.py", line 21, in <module>
    send_to_twitter()
  File "C:\Python32\AB3.py", line 19, in send_to_twitter
    resp = urllib.request.urlopen('http://twitter.com/statuses/update.json', params)
  File "C:\Python32\lib\urllib\request.py", line 138, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python32\lib\urllib\request.py", line 367, in open
    req = meth(req)
  File "C:\Python32\lib\urllib\request.py", line 1066, in do_request_
    raise TypeError("POST data should be bytes"
TypeError: POST data should be bytes or an iterable of bytes. It cannot be str.

Sounds like trying run Python2 code in Python3, did you try to run 2to3.py script for the code? Try changing strings to bytes: b''

Sounds like trying run Python2 code in Python3, did you try to run 2to3.py script for the code? Try changing strings to bytes: b''

I'm not seeing 2to3.py and I'm not sure how to convert strings to bytes

I'm not seeing 2to3.py and I'm not sure how to convert strings to bytes

okay sorry Pytony, I figured out the str to bytes thing, and now I'm getting bytes has no attribute data

sorry Tony, I'm feeling like an idiot, no matter how I try to run that in IDLE it's giving me Invalid Syntax

You run it from command line, not from IDLE:

It can be converted to Python 3.x code via 2to3 on the command line:

$ 2to3 example.py

2to3.py is located in <Python32 install dir>\Tools\Scripts directory, you may need to give the command as

python  <Python32 install dir>\Tools\Scripts\2to3.py <name of the file>

if that directory is not in your path. If you want to be able to ignore .py extension from invokingscripts, you can put .PY to PATHEXT if you are using Windows.

should it look like

import sys
from lib2to3.main import main
type_fix
sys.exit(main("lib2to3.fixes"))

?

Edited 5 Years Ago by pyguy62: forgot line of code

I do not understand whath you are trying to do, also I hate the way you write whips = int(1) instead of simply whips = 1. Also you do not put functions first and import math even you are not using it etc. Are you Java coder?

Edited 5 Years Ago by pyTony: n/a

just trying to be thorough, but if it doesn't need to be done then thank you for correcting me. I'm trying to send the result of the equation to twitter, however I'm having tons of issues with it. I tried 2to3 now, and also just running the send_to_twitter function in python 2, it's not working either way.

This article has been dead for over six months. Start a new discussion instead.