0

I am testing some scenarios with SQL Server Machine learning. I was trying to query an external Web Service from inside a Python script in SQL Server Management Studio. Something like this:

EXEC sp_execute_external_script
@language =N'Python', 
@script=N'
import os
os.system("python \"C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\Lib\cust\ml.py\"")

OutputDataSet = InputDataSet
print("Say Hello from SQL Server Management Studio")
',
@input_data_1 =N'SELECT 42' 
WITH RESULT SETS (([TheAnswer] int not null));  
GO 

The ml.py script looks like (simplified):

#!"C:\Program Files\Microsoft SQL Server\MSSQL15.MSSQLSERVER\PYTHON_SERVICES\python.exe"
import pandas as pd
import urllib3

http = urllib3.PoolManager()
response  = http.request('GET', 'https://example.com/myWS.json')

df = pd.read_csv(response.data)  # Web Service returns a plain csv like payload.

# Keep doing something interesting

When I run ml.py on the console, it runs just fine. However, when I try to run from SQL Server Management Studio I get the following error:

Msg 39019, Level 16, State 2, Line 1
An external script error occurred: 
SqlSatelliteCall error: Error in execution.  Check the output for more information.
STDOUT message(s) from external script: 
There was an error getting the leads fields: HTTPSConnectionPool(host='example.com', port=443): Max retries exceeded with url: /myWS.json (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000002076ECACC18>: Failed to establish a new connection: [WinError 10013] An attempt to access a socket in a way forbidden by its access permissions was attempted'))

So, it is clear there are some communications permissions to set but I am not sure where I am supposed to set them.

My objective is to insert the results of the Web Service directly into a SQL query without having to import from an external file. I am not sure this is the best approach but I just wanted to test to see its viability.

Any idea is welcome.

Thank you!

Wilmar
  • 558
  • 1
  • 5
  • 16
  • You're not trying to run that script from SSMS, you're trying to run it on an SQL Server. (SSMS is just a client application, the work happens on the SQL Server.) On the machine running SQL Server have you granted egress permissions to python and SQL Server through your software firewall? (e.g.: Windows Firewall) – AlwaysLearning Dec 28 '20 at 23:39
  • Hi @AlwaysLearning, thank you for your comment. Yes, I have allowed Python to make requests through the FW, as I mentioned, when I run the python script alone through the console it works. I am not sure how to grant permission to SQL server though. I will check that out. – Wilmar Dec 29 '20 at 12:03

0 Answers0