How to do Proxy setting in RTH REST API using Python

I am getting below error now in python when i try to use for RTH REST Api:

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Is it related to proxy ? how to provide proxy setting in python in order to do extraction for TRTH REST Api ?

Below is part of the code which is almost same like given in tutorial :

# Imports:
import requests
import json
import shutil
import time
import urllib3
import gzip

# ====================================================================================
# Step 1: token request

reqStart = "https://selectapi.datascope.refinitiv.com/RestApi/v1"
requestUrl = reqStart + "/Authentication/RequestToken"

requestHeaders = {
"Prefer": "respond-async",
"Content-Type": "application/json"
}

requestBody = {
"Credentials": {
"Username": myUsername,
"Password": myPassword
}
}

r1 = requests.post(requestUrl, json=requestBody, headers=requestHeaders)

if r1.status_code == 200:
jsonResponse = json.loads(r1.text.encode('ascii', 'ignore'))
token = jsonResponse["value"]
print('Authentication token (valid 24 hours):')
print(token)
else:
print('Replace myUserName and myPassword with valid credentials, then repeat the request')

# In[7]:

# Step 2: send an on demand extraction request using the received token

requestUrl = reqStart + '/Extractions/ExtractRaw'

requestHeaders = {
"Prefer": "respond-async",
"Content-Type": "application/json",
"Authorization": "token " + token
}

requestBody = {
"ExtractionRequest": {
"@odata.type": "#DataScope.Select.Api.Extractions.ExtractionRequests.TickHistoryRawExtractionRequest",
"IdentifierList": {
"@odata.type": "#DataScope.Select.Api.Extractions.ExtractionRequests.InstrumentIdentifierList",
"InstrumentIdentifiers": [{
"Identifier": "CARR.PA",
"IdentifierType": "Ric"
}]
},
"Condition": {
"MessageTimeStampIn": "GmtUtc",
"ReportDateRangeType": "Range",
"QueryStartDate": "2016-09-29T12:00:00.000Z",
"QueryEndDate": "2016-09-29T12:10:00.000Z",
"ExtractBy": "Ric",
"SortBy": "SingleByRic",
"DomainCode": "MarketPrice",
"DisplaySourceRIC": "true"
}
}
}

r2 = requests.post(requestUrl, json=requestBody, headers=requestHeaders)
r3 = r2

# Display the HTTP status of the response
# Initial response status (after approximately 30 seconds wait) is usually 202
status_code = r2.status_code
print("HTTP status of the response: " + str(status_code))

# In[8]:

# Step 3: if required, poll the status of the request using the received location URL.
# Once the request has completed, retrieve the jobId and extraction notes.

# If status is 202, display the location url we received, and will use to poll the status of the extraction request:
if status_code == 202:
requestUrl = r2.headers["location"]
print('Extraction is not complete, we shall poll the location URL:')
print(str(requestUrl))

requestHeaders = {
"Prefer": "respond-async",
"Content-Type": "application/json",
"Authorization": "token " + token
}

# As long as the status of the request is 202, the extraction is not finished;
# we must wait, and poll the status until it is no longer 202:
while (status_code == 202):
print('As we received a 202, we wait 30 seconds, then poll again (until we receive a 200)')
time.sleep(30)
r3 = requests.get(requestUrl, headers=requestHeaders)
status_code = r3.status_code
print('HTTP status of the response: ' + str(status_code))

# When the status of the request is 200 the extraction is complete;
# we retrieve and display the jobId and the extraction notes (it is recommended to analyse their content)):
if status_code == 200:
r3Json = json.loads(r3.text.encode('ascii', 'ignore'))
jobId = r3Json["JobId"]
print('\njobId: ' + jobId + '\n')
notes = r3Json["Notes"]
print('Extraction notes:\n' + notes[0])

# If instead of a status 200 we receive a different status, there was an error:
if status_code != 200:
print('An error occured. Try to run this cell again. If it fails, re-run the previous cell.\n')

Best Answer

  • Gurpreet
    Gurpreet admin
    Answer ✓

    Hello @rahul.deshmukh ,

    If your network uses proxy server and you haven't explicitly provided proxy information to python modules, then your application will get connection timeouts.

    To provide proxy server information insert these lines at the top of Step 1 in the code:

    import os
    os.environ["HTTPS_PROXY"] = "http://YOUR_PROXY_SERVER_HOST:PROXY_PORT"

Answers