exceptions.TypeError an integer is required

Discussion in 'Python' started by jakecjacobson, Jul 24, 2009.

  1. I am trying to do a post to a REST API over HTTPS and requires the
    script to pass a cert to the server. I am getting
    "exceptions.TypeError an integer is required" error and can't find the
    reason. I commenting out the lines of code, it is happening on the
    connection.request() line. Here is the problem code. Would love some
    help if possible.

    head = {"Content-Type" : "application/x-www-form-urlencoded",
    "Accept" : "text/plain"}
    parameters = urlencode({"collection" : collection, "entryxml" : open
    (file,'r').read()})
    try:
    connection = httplib.HTTPSConnection(host, port, key_file,
    cert_file)
    connection.request('POST', path, parameters, head)
    response = connection.getresponse()
    print response.status, response.reason
    except:
    print sys.exc_type, sys.exc_value

    connection.close()
    jakecjacobson, Jul 24, 2009
    #1
    1. Advertising

  2. On Fri, 24 Jul 2009 11:24:58 -0700, jakecjacobson wrote:

    > I am trying to do a post to a REST API over HTTPS and requires the
    > script to pass a cert to the server. I am getting "exceptions.TypeError
    > an integer is required" error and can't find the reason. I commenting
    > out the lines of code, it is happening on the connection.request() line.
    > Here is the problem code. Would love some help if possible.


    Please post the traceback that you get.

    My guess is that you are passing a string instead of an integer, probably
    for the port.


    [...]
    > except:
    > print sys.exc_type, sys.exc_value


    As a general rule, a bare except of that fashion is bad practice. Unless
    you can explain why it is normally bad practice, *and* why your case is
    an exception (no pun intended) to the rule "never use bare except
    clauses", I suggest you either:

    * replace "except:" with "except Exception:" instead.

    * better still, re-write the entire try block as:


    try:
    [code goes here]
    finally:
    connection.close()

    and use the Python error-reporting mechanism instead of defeating it.



    --
    Steven
    Steven D'Aprano, Jul 24, 2009
    #2
    1. Advertising

  3. jakecjacobson

    Terry Reedy Guest

    jakecjacobson wrote:
    > I am trying to do a post to a REST API over HTTPS and requires the
    > script to pass a cert to the server. I am getting
    > "exceptions.TypeError an integer is required" error and can't find the
    > reason. I commenting out the lines of code, it is happening on the
    > connection.request() line. Here is the problem code. Would love some
    > help if possible.
    >
    > head = {"Content-Type" : "application/x-www-form-urlencoded",
    > "Accept" : "text/plain"}
    > parameters = urlencode({"collection" : collection, "entryxml" : open
    > (file,'r').read()})
    > try:
    > connection = httplib.HTTPSConnection(host, port, key_file,
    > cert_file)
    > connection.request('POST', path, parameters, head)
    > response = connection.getresponse()
    > print response.status, response.reason
    > except:
    > print sys.exc_type, sys.exc_value
    >
    > connection.close()


    Help us help you by posting the full actual traceback.
    Terry Reedy, Jul 24, 2009
    #3
  4. On Jul 24, 3:11 pm, Steven D'Aprano <st...@REMOVE-THIS-
    cybersource.com.au> wrote:
    > On Fri, 24 Jul 2009 11:24:58 -0700, jakecjacobson wrote:
    > > I am trying to do a post to a REST API over HTTPS and requires the
    > > script to pass a cert to the server.  I am getting "exceptions.TypeError
    > > an integer is required" error and can't find the reason.  I commenting
    > > out the lines of code, it is happening on the connection.request() line..
    > >  Here is the problem code.  Would love some help if possible.

    >
    > Please post the traceback that you get.
    >
    > My guess is that you are passing a string instead of an integer, probably
    > for the port.
    >
    > [...]
    >
    > >    except:
    > >            print sys.exc_type, sys.exc_value

    >
    > As a general rule, a bare except of that fashion is bad practice. Unless
    > you can explain why it is normally bad practice, *and* why your case is
    > an exception (no pun intended) to the rule "never use bare except
    > clauses", I suggest you either:
    >
    > * replace "except:" with "except Exception:" instead.
    >
    > * better still, re-write the entire try block as:
    >
    >     try:
    >         [code goes here]
    >     finally:
    >         connection.close()
    >
    > and use the Python error-reporting mechanism instead of defeating it.
    >
    > --
    > Steven


    Steven,

    You are quite correct in your statements. My goal was not to make
    great code but something that I could quickly test. My assumption was
    that the httplib.HTTPSConnection() would do the cast to int for me.
    As soon as I cast it to an int, I was able to get past that issue.

    Still not able to post because I am getting a bad cert error.

    Jake Jacobson
    jakecjacobson, Jul 27, 2009
    #4
  5. En Mon, 27 Jul 2009 12:44:40 -0300, jakecjacobson
    <> escribió:
    >
    > You are quite correct in your statements. My goal was not to make
    > great code but something that I could quickly test. My assumption was
    > that the httplib.HTTPSConnection() would do the cast to int for me.
    > As soon as I cast it to an int, I was able to get past that issue.


    A few remarks that may help learning the language:

    Note that Python is a strongly typed (and dynamic) language. All objects
    have a defined type: "443" is not the same thing as 443, and "2" + 2
    raises a TypeError.

    If a function expects an integer, you must provide an integer (or
    something that at least "acts" as an integer; a string isn't
    "integer-alike" at all from Python's POV)

    Also, you don't "cast" an object into another: the expression int("443")
    is a constructor, and it returns a new object (an integer) based upon its
    argument. (so it's quite different from, say, casting "short" to "unsigned
    short" in C, that only changes the way the compiler treats the same bytes
    in memory).

    --
    Gabriel Genellina
    Gabriel Genellina, Jul 28, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ahsan Imam
    Replies:
    4
    Views:
    5,683
    Josiah Carlson
    Feb 7, 2004
  2. Andi Clemens
    Replies:
    8
    Views:
    647
    Tim Heaney
    Sep 17, 2006
  3. jeni
    Replies:
    3
    Views:
    900
    Ulrich Eckhardt
    Jun 16, 2009
  4. MRAB
    Replies:
    1
    Views:
    526
    Nobody
    Nov 22, 2009
  5. Dave Angel

    Re: TypeError: an integer is required

    Dave Angel, Nov 22, 2009, in forum: Python
    Replies:
    0
    Views:
    1,889
    Dave Angel
    Nov 22, 2009
Loading...

Share This Page