-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix an issue where TDK servers were blocking Python urllib requests
Requests with user agent strings that contained "Python" were blocked. Using a common user agent string solved the issue. Also added some other common headers to avoid blocked by heuristics in the future. Closes: #1
- Loading branch information
Showing
3 changed files
with
29 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
|
||
setuptools.setup( | ||
name="tdk-py", | ||
version="1.1.0.post1", | ||
version="1.1.1", | ||
author="Emre Özcan", | ||
author_email="[email protected]", | ||
description="Python API for the Turkish Language Foundation", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import urllib.request | ||
|
||
_http_headers = { | ||
"Accept": "application/json, text/javascript, */*; q=0.01", | ||
"Referer": "https://sozluk.gov.tr/", | ||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " | ||
"AppleWebKit/537.36 (KHTML, like Gecko) " | ||
"Chrome/99.0.4844.51 " | ||
"Safari/537.36", | ||
"X-Requested-With": "XMLHttpRequest", | ||
} | ||
|
||
|
||
def make_request(*args, **kwargs): | ||
""" | ||
Helper function to add default headers to a urllib request. | ||
All arguments are passed down to urllib.request.Request, | ||
Default headers are added if the "headers" keyword argument is not given. | ||
""" | ||
if "headers" not in kwargs: | ||
kwargs["headers"] = _http_headers | ||
return urllib.request.urlopen(urllib.request.Request(*args, **kwargs)) |