exceptions.TypeError an integer is required

J

jakecjacobson

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()
 
S

Steven D'Aprano

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.
 
T

Terry Reedy

jakecjacobson said:
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.
 
J

jakecjacobson

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,

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
 
G

Gabriel Genellina

En Mon, 27 Jul 2009 12:44:40 -0300, jakecjacobson
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).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top