Search code examples
python-3.xcx-freeze

URLFetch is not available in this environment - cx_Freeze


I am packaging my app with cx_Freeze. I am using requests.post to fetch the JSON, but I am getting an error.

AppEnginePlatformError: URLFetch is not available in this environment.

from base64 import b64encode
import json
import requests

api_key = **************
ENDPOINT_URL = 'https://vision.googleapis.com/v1/images:annotate'

with open(row.path, 'rb') as f:
print(row.path)
ctxt = b64encode(f.read()).decode()
img_requests.append({
        'image': {'content': ctxt},
        'features': [{
            'type': 'TEXT_DETECTION',
            'maxResults': 1
        }]
})
img_encode = json.dumps({"requests": img_requests }).encode()
response = requests.post(ENDPOINT_URL,data=img_encode, verify=True,params={'key': api_key},
                         headers={'Content-Type': 'application/json'})

What should I do, instead of requests.post should I use urllib or something else?


Solution

  • I used urllib & it is working fine now.

    Code:

    hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
                'Accept-Encoding': 'none',
                'Accept-Language': 'en-US,en;q=0.8',
                'Connection': 'keep-alive'}
    req = urllib.request.Request(url=ENDPOINT_URL + api_key, headers=hdr)
    req.add_header('Content-Type', 'application/json')
    response = urllib.request.urlopen(req, img_encode)