system.net.httpPost error codes

Would it be idiomatic to read error codes from the system.net.httpPost IOError exception, or is there a way to retrieve the HTTP status code?

As an example:

[code]from system.net import httpPost

acct = u’username’
auth = u’password’
url = ‘https://www.google.com

try:
resp = httpPost(url, username=acct, password=auth)
print resp
except IOError, e:
print e[/code]
yields

Server returned HTTP response code: 405 for URL: https://www.google.com

I can take the output e and parse it (or sledge it with a regex), but before I start messing with strings, I figured I’d check if I just missed an obvious way to just extract the HTTP status code. Thanks!

No obvious way to extract just the HTTP status code. Hammer away.

Best,

Well, you know what they say…

Here’s a playground script of what I was thinking:

[code]import re

pattern = ‘HTTP response code: ([0-9]{3}) for URL: (http[s]?://.*)’
source = ‘Server returned HTTP response code: 405 for URL: https://www.google.com

regex = re.compile(pattern)

match = regex.findall(source)

for m in match:
print m[/code]
yielding

('405', 'https://www.google.com')

It’ll work, but I can’t help but feel it’s a bit heavy-handed for the job. I could just do some string splicing, but I don’t know if I should count on the string being perfectly identically formatted for all errors.

Yes. Another approach is to use the builtin Python library module for making http requests: urllib2

Or use Java’s HttpURLConnection class.

Remember you have Python and Java libraries available to you in Python scripts.

Best,

Does one approach have any significant advantages?

Getting SSL to work right without calling upon Java is hard (possible in Jython 2.5.3?), so I figured it would be more reliable to use the built-in function. I also guessed there may be some nice side effects, like Ignition handling some connection pooling or the like. And it’s somewhat turn-key (except for error handling, which is going to be non-optimal anyway).

But since I’ll be making quite a few HTTP requests regularly, it wouldn’t be a terrible idea to optimize this early.