[New-bugs-announce] [issue25037] ValueError: invalid literal for int() with base 16: b'[\r\n'
Alan
report at bugs.python.org
Wed Sep 9 05:43:10 CEST 2015
New submission from Alan:
I've written a piece of code to POST request to a web service.
===========================================================================
import json
import urllib
from urllib import request
from urllib import parse
def Payload(start_date, end_date, pnode_list):
payload = {"startDate": start_date,
"endDate": end_date,
"pnodelist": pnode_list}
return json.dumps(payload)
def DownloadData(url, payload, header):
data = []
request = urllib.request.Request(url, payload, header)
try:
response = urllib.request.urlopen(request)
except urllib.error.URLError as e:
print("URLError occured.")
except urllib.error.HTTPError as e:
print("HTTPError occured.")
else:
#response.chunked = False #if this line is commented, ValueError will be thrown...
data = json.loads(response.read().decode("utf-8"))
return data
def main():
url = "https://dataminer.pjm.com/dataminer/rest/public/api/markets/dayahead/lmp/daily"
payload = Payload("2015-07-01", "2015-07-01", [135389795])
header = {"Content-Type": "application/json"}
data = DownloadData(url, payload.encode("utf-8"), header)
print(data)
if __name__ == "__main__":
main()
===========================================================================
However, "ValueError:invalid literal for int() with base 16: b'[\r\n'" is thrown when the HTTPResponse.read() is invoked:
Traceback (most recent call last):
File "C:\Python34\lib\http\client.py", line 587, in _readall_chunked
chunk_left = self._read_next_chunk_size()
File "C:\Python34\lib\http\client.py", line 559, in _read_next_chunk_si
return int(line, 16)
ValueError: invalid literal for int() with base 16: b'[\r\n'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "textpjm.py", line 34, in <module>
main()
File "textpjm.py", line 30, in main
data = DownloadData(url, payload.encode("utf-8"), header)
File "textpjm.py", line 23, in DownloadData
data = json.loads(response.read().decode("utf-8"))
File "C:\Python34\lib\http\client.py", line 506, in read
return self._readall_chunked()
File "C:\Python34\lib\http\client.py", line 591, in _readall_chunked
raise IncompleteRead(b''.join(value))
http.client.IncompleteRead: IncompleteRead(0 bytes read)
I've found a solution to avoid this exception: before HTTPResponse.read() is called, I have to set HTTPResponse.chunked to be False!
I wonder if there's something wrong in HTTPResponse.
----------
components: Library (Lib)
files: testpjm.py
messages: 250275
nosy: alan
priority: normal
severity: normal
status: open
title: ValueError: invalid literal for int() with base 16: b'[\r\n'
type: crash
versions: Python 3.4
Added file: http://bugs.python.org/file40416/testpjm.py
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25037>
_______________________________________
More information about the New-bugs-announce
mailing list