sql server 2017 ml services python - access a socket in a way forbidden by its access permissions

0

SOLUTION: After three days of looking for answer we disable our Firewall in domain. After that script worked. Then we figure out that you need to add specific programs as exception to windows firewall. We added following exceptions and it works for us:

Python.exe in python services:

C:\Program Files\Microsoft SQL Server\140\PYTHON_SERVER\python.exe
C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\PYTHON_SERVICES\python.exe

SQL Server Telemetry client:

C:\Program Files\Microsoft SQL 
Server\MSSQL14.MSSQLSERVER\MSSQL\Binn\sqlceip.exe
C:\Program Files\Microsoft SQL Server\140\DTS\Binn\sqlceip.exe

PROBLEM:

I try to run Python code on SQL Server 2017 with Python Services (in-Database) using requests library to get data from external service but i fail to connect to any url (I get WinError 10013).

My code:

EXECUTE sp_execute_external_script
@language = N'Python'
, @script = N'
import requests as r
url="https://google.com"
response = r.get(url)
print(response.status_code)
'

When I run python code in python shell I get correct response, status code "200". But when I try to run same code in SQL Server using sp_execute_external_script I get error message.Crucial part is:

 [WinError 10013] An attempt was made to access a socket in a way forbidden 
 by its access permissions',))

I tried already several things:

  1. I checked if other Python scripts run propetly (they do),

  2. I checked if script run in Python shell (it does),

  3. Then I checked firewall outbound and inbound rules for port 443 in firewall setting on server (it is generally open),

  4. Then I tried to google solution or find anything in microsoft documentation. I found info that probably user which run the script lack some permissions: What causes Python socket error?

  5. Then I try to figure out how to find user used by SQL Server. If I'm correct it's SQL Server Launchpad, to be more precise user for this service:

https://docs.microsoft.com/en-us/sql/advanced-analytics/security/sql-server-launchpad-service-account?view=sql-server-2017 https://docs.microsoft.com/en-us/sql/advanced-analytics/common-issues-external-script-execution?view=sql-server-2017

I checked name in SSCM and it is:

NT Service\MSSQLLaunchpad$MSSQL
  1. Then I try to give some privliges for this user using powerShell as desribe here: https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/configuring-http-and-https

I try to run this command:

netsh http add urlacl url=https://+:443/MyUri user='NT 
Service\MSSQLLaunchpad$MSSQL'

But without any results.

Just in case here is complete error message:

Msg 39004, Level 16, State 20, Line 0
A 'Python' script error occurred during execution of 'sp_execute_external_script' with HRESULT 0x80004004.
Msg 39019, Level 16, State 2, Line 0
An external script error occurred: 

Error in execution.  Check the output for more information.
Traceback (most recent call last):
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\packages\urllib3\connection.py", line 141, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\packages\urllib3\util\connection.py", line 83, in create_connection
    raise err
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\packages\urllib3\util\connection.py", line 73, in create_connection
    sock.connect(sa)
OSError: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

Msg 39019, Level 16, State 2, Line 0
An external script error occurred: 
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 345, in _make_request
    self._validate_conn(conn)
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 844, in _validate_conn
    conn.connect()
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\packages\urllib3\connection.py", line 284, in connect
    conn = self._new_conn()
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\packages\urllib3\connection.py", line 150, in _new_conn

Msg 39019, Level 16, State 2, Line 0
An external script error occurred: 
    self, "Failed to establish a new connection: %s" % e)
requests.packages.urllib3.exceptions.NewConnectionError: <requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x000002438F92A940>: Failed to establish a new connection: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\adapters.py", line 423, in send
    timeout=timeout
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\packages\urllib3\connectionpool.py", line 649, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\packages\urllib3\util\retry.py", line 376, in increment

Msg 39019, Level 16, State 2, Line 0
An external script error occurred: 
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
requests.packages.urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='google.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x000002438F92A940>: Failed to establish a new connection: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 5, in <module>
  File "C:\PROGRA~1\MICROS~1\MSSQL1~2.MSS\MSSQL\EXTENS~1\MSSQL01\22F2EE59-D365-4A6E-9C7C-93741F6814BF\sqlindb.py", line 39, in transform
    response = r.get(url)
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\api.py", line 70, in get
    return request('get', url, params=params, **kwargs)

Msg 39019, Level 16, State 2, Line 0
An external script error occurred: 
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\requests\adapters.py", line 487, in send
    raise ConnectionError(e, request=request)

Msg 39019, Level 16, State 2, Line 0
An external script error occurred: 
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='google.com', port=443): Max retries exceeded with url: / (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x000002438F92A940>: Failed to establish a new connection: [WinError 10013] An attempt was made to access a socket in a way forbidden by its access permissions',))

SqlSatelliteCall error: Error in execution.  Check the output for more information.
STDOUT message(s) from external script: 
SqlSatelliteCall function failed. Please see the console output for more information.
Traceback (most recent call last):
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\revoscalepy\computecontext\RxInSqlServer.py", line 406, in rx_sql_satellite_call
    rx_native_call("SqlSatelliteCall", params)
  File "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQL\PYTHON_SERVICES\lib\site-packages\revoscalepy\RxSerializable.py", line 291, in rx_native_call
    ret = px_call(functionname, params)
RuntimeError: revoscalepy function failed.

EDIT:

On my local computer actually everything worked fine and code is executing with excepted result (it return status code 200). So it's problem with running it on server. I'm using Microsft Server 2012. I try on two different servers and result is the same.

python
sql-server
tsql
sockets
asked on Stack Overflow Jan 30, 2019 by hamnis • edited Feb 1, 2019 by hamnis

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0