我正在使用 SQL Server 机器学习测试一些场景。我试图从 SQL Server Management Studio 的 Python 脚本中查询外部 Web 服务。像这样的东西:
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
该ml.py
脚本看起来像(简化):
#!"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
当我ml.py
在控制台上运行时,它运行得很好。但是,当我尝试从 SQL Server Management Studio 运行时,出现以下错误:
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'))
所以,很明显有一些通信权限需要设置,但我不确定我应该在哪里设置它们。
我的目标是将 Web 服务的结果直接插入到 SQL 查询中,而无需从外部文件导入。我不确定这是最好的方法,但我只是想测试一下它的可行性。
欢迎任何想法。
谢谢!