Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > exceptions.TypeError an integer is required

Reply
Thread Tools

exceptions.TypeError an integer is required

 
 
jakecjacobson
Guest
Posts: n/a
 
      07-24-2009
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()
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-24-2009
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
 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      07-24-2009
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.

 
Reply With Quote
 
jakecjacobson
Guest
Posts: n/a
 
      07-27-2009
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
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      07-28-2009
En Mon, 27 Jul 2009 12:44:40 -0300, jakecjacobson
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I add an Integer to another Integer? Sebastian Stelzer Java 6 04-07-2010 07:03 PM
CType(x,Integer) vs. Integer.Parse(x) =?Utf-8?B?Sm9l?= ASP .Net 7 02-07-2006 02:30 AM
how do I make Class.forName("Integer") returning java.lang.Integer? Johannes Zellner Java 22 12-19-2005 11:22 AM
How do I add an Integer to another Integer? Sebastian Stelzer Java 2 10-15-2004 01:17 PM
No Math.min(Integer, Integer)? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 0 07-29-2003 07:46 PM



Advertisments