1

I'm trying to figure out how to get proper english/ascii error message from a failes requests.get. for some reason (probably being working with a hebrew locale), I get a jibrish unicode error message. I can deocde the message and display it correctly in hebrew, but I really prefer (for various reasons) that the Exception error text would be in english.

checking Requests Exception source code doesn't even lead me in the right direction, Since I seem to be I'm getting a different exception: (10060, some encoded error text,') and the class is: <class 'socket.error'>.

So I guess we are probably looking at a socket.error from socket lib. At least as far as I can tell from parsing the Exception args (I can't raise the exception directly, only while running the app, where I do a smelly except Exception as e and parse e.args and e.message).

EDIT: this is the results I get from parsing e.args, e.message, e.__class__

(10060, '\xfe\xfe\xf0\xe9\xf1\xe9\xe5\xef \xe7\xe9\xe1\xe5\xf8 \xf0\xeb\xf9\xec \xee\xe0\xe7\xf8 \xf9\xe4\xf6\xe3 \xe4\xee\xe7\xe5\xe1\xf8 \xec\xe0 \xe4\xe2\xe9\xe1 \xeb\xf8\xe0\xe5\xe9 \xec\xe0\xe7\xf8 \xfa\xf7\xe5\xf4\xfa \xe6\xee\xef,'), message: , class: <class 'socket.error'>

the string is encoded 'windows-1255' and decodes to (hebrew):

‏‏ניסיון חיבור נכשל מאחר שהצד המחובר לא הגיב כראוי לאחר תקופת זמן,

tranlates to: "connection attempt failed since the connected side didn't respond properly after a time period"

the code raising the Exception:

try:
    print ("parsing json from:", srvstr)
    getjson = requests.get(srvstr, params=params, auth=(user, password), verify=ssl)
    print('url:', getjson.url)
except Exception as e:
    args = ", ".join(args)
    details = 'args: %s, message: %s, class: %s' % (args, e.message, e.__class__)

So:

how do I set the locale so that requests (or the underlying socket lib if my guess is correct) would return an english/Ascii error message?

Thanks for the help!

4

1 回答 1

0

您可以尝试os.strerror,但它可能不会返回任何内容或相同的非英文字符串。

这个硬编码的英文是从这里刮下来的:http: //support.microsoft.com/kb/819124

ENGLISH_WINDOWS_SOCKET_MESSAGES = {
    10004: "Interrupted function call.",
    10013: "Permission denied.",
    10014: "Bad address.",
    10022: "Invalid argument.",
    10024: "Too many open files.",
    10035: "Resource temporarily unavailable.",
    10036: "Operation now in progress.",
    10037: "Operation already in progress.",
    10038: "Socket operation on nonsocket.",
    10039: "Destination address required.",
    10040: "Message too long.",
    10041: "Protocol wrong type for socket.",
    10042: "Bad protocol option.",
    10043: "Protocol not supported.",
    10044: "Socket type not supported.",
    10045: "Operation not supported.",
    10046: "Protocol family not supported.",
    10047: "Address family not supported by protocol family.",
    10048: "Address already in use.",
    10049: "Cannot assign requested address.",
    10050: "Network is down.",
    10051: "Network is unreachable.",
    10052: "Network dropped connection on reset.",
    10053: "Software caused connection abort.",
    10054: "Connection reset by peer.",
    10055: "No buffer space available.",
    10056: "Socket is already connected.",
    10057: "Socket is not connected.",
    10058: "Cannot send after socket shutdown.",
    10060: "Connection timed out.",
    10061: "Connection refused.",
    10064: "Host is down.",
    10065: "No route to host.",
    10067: "Too many processes.",
    10091: "Network subsystem is unavailable.",
    10092: "Winsock.dll version out of range.",
    10093: "Successful WSAStartup not yet performed.",
    10101: "Graceful shutdown in progress.",
    10109: "Class type not found.",
    11001: "Host not found. No such host is known.",
    11002: "Nonauthoritative host not found.",
    11003: "This is a nonrecoverable error.",
    11004: "Valid name, no data record of requested type.",
}

(对于未来的爬虫:使用了以下 xpath://div[@id="MT1"]//h3following-sibling::ul/li

于 2013-08-17T03:52:17.013 回答