IHTSDO / SNOMED-in-5-minutes

Easy-to-use tutorials for accessing SNOMED APIs within 5 min using various programming languages
Other
123 stars 56 forks source link

Add User-Agent to header in Python3 examples to prevent IP blocked me… #24

Closed cjezzerc closed 2 years ago

cjezzerc commented 2 years ago

The file python3-examples/example.py was generating this error

CYGWIN> python3 examples.py Traceback (most recent call last): File "/cygdrive/c/Users/jeremy/GIT/SNOMED-in-5-minutes/python3-examples/examples.py", line 41, in getConceptById('109152007') File "/cygdrive/c/Users/jeremy/GIT/SNOMED-in-5-minutes/python3-examples/examples.py", line 13, in getConceptById data = json.loads(response.decode('utf-8')) File "/usr/lib/python3.9/json/init.py", line 346, in loads return _default_decoder.decode(s) File "/usr/lib/python3.9/json/decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.9/json/decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I traced it first to the fact that the response received was not valid json, but was an html page saying my IP was blocked.

As access via web browser still worked, I tried curl and that also worked.

So I checked the request header curl was sending and tried setting User-Agent in the header python was sending and that seemed to cure it.

I have added a function "url_open_with_header" which replaces the calls to the plain "urlopen" in the main body.

The output is now:

CYGWIN> python3 examples.py Bilirubin test kit (physical object) Methylphenyltetrahydropyridine (substance) 471023 864

rorydavidson commented 2 years ago

Thanks for finding this and adding the fix!

cjezzerc commented 2 years ago

Thanks for responding so quickly.

Regards

Jeremy

On Tue, May 24, 2022 at 1:07 PM Rory Davidson @.***> wrote:

Thanks for finding this and adding the fix!

— Reply to this email directly, view it on GitHub https://github.com/IHTSDO/SNOMED-in-5-minutes/pull/24#issuecomment-1135833509, or unsubscribe https://github.com/notifications/unsubscribe-auth/AX5KOJVB6LBBSKXRJYMU3SDVLTBB3ANCNFSM5WZH5ESA . You are receiving this because you authored the thread.Message ID: @.***>