Running Flask raises "WinError 10038" - python

Running a simple Flask app raises OSError: [WinError 10038] An operation was attempted on something that is not a socket. The page is not served in the browser. What does the error mean? How do I fix it?
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def index():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 294-294-951
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\kdalal\AppData\Local\Continuum\Anaconda3\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\kdalal\AppData\Local\Continuum\Anaconda3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\kdalal\AppData\Local\Continuum\Anaconda3\lib\site-packages\werkzeug\serving.py", line 670, in inner
fd=fd)
File "C:\Users\kdalal\AppData\Local\Continuum\Anaconda3\lib\site-packages\werkzeug\serving.py", line 564, in make_server
passthrough_errors, ssl_context, fd=fd)
File "C:\Users\kdalal\AppData\Local\Continuum\Anaconda3\lib\site-packages\werkzeug\serving.py", line 474, in __init__
socket.SOCK_STREAM)
File "C:\Users\kdalal\AppData\Local\Continuum\Anaconda3\lib\socket.py", line 460, in fromfd
nfd = dup(fd)
OSError: [WinError 10038] An operation was attempted on something that is not a socket

This issue was fixed in Werkzeug 0.12.1. Upgrade to the latest version:
pip install -U werkzeug

Today, September 2022, I am using Werkzeug 2.2.2 and I was getting exactly the same issue:
OSError: [WinError 10038] An operation was attempted on something that is not a socket
I tried using the environment variables FLASK_DEBUG and/or FLASK_ENVIRONMENT, since this error only appeared when I run the Flask app in debug mode, but I kept getting this socket error. Really frustrating not being able to use Flask in debug mode.
After more researching, I found the solution. I opened the command prompt (cmd) as an Administrator, and ran the following command netsh winsock reset then restarted the computer. The error doesn't appear anymore when I run Flask in debug mode now.
It would seem this is an issue only affecting those still with Vista or Windows 7, when the Winsock2 gets corrupted. More details can be found in these links:
how-and-why-to-perform-a-netsh-winsock-reset-on-windows
Microsoft support - Reset Winsock for Windows Vista
Original solution found on Karatos
Hopefully, this can be useful to someone with the same problem.

Related

Unable to run Django Test Server in Ubuntu Server 16.04

I have 2 servers. I am attempting to setup one for continuous integration for my main website server
Web server 1(cloud-hosting):
Python3.6
Django3.1
Ubuntu16.04
Webserver 2(VPS):
Python3.7
Django3.1
Ubuntu16.04
Jenkins
--ShiningPanda(plugin)
Im new to web development, so if it seems odd as far as my web server types, that is why. I have been following along in the book Test Driven Development with Python. My issue is that when running python manage.py test [app] My [app] inherits from the class StaticLiveSever to generate a testing environment. On webserver 1, this works fine. On webserver 2, i get an error that the request address cannot be assigned. I use jenkins to build the environment, but the error i get is OSerror[99]:cannot assign requested address. I dont understand why this is happening when i run the same commands in Web Sever 1. It runs fine. Although again, the commands are run by jenkins and jenkins is configured to run python3.7
Full Traceback(Main Issue)
Traceback (most recent call last):
File "/var/lib/jenkins/shiningpanda/jobs/ddc1aed1/virtualenvs/d41d8cd9/lib/python3.7/site-packages/django/test/testcases.py", line 1449, in setUpClass
raise cls.server_thread.error
File "/var/lib/jenkins/shiningpanda/jobs/ddc1aed1/virtualenvs/d41d8cd9/lib/python3.7/site-packages/django/test/testcases.py", line 1374, in run
self.httpd = self._create_server()
File "/var/lib/jenkins/shiningpanda/jobs/ddc1aed1/virtualenvs/d41d8cd9/lib/python3.7/site-packages/django/test/testcases.py", line 1389, in _create_server
return ThreadedWSGIServer((self.host, self.port), QuietWSGIRequestHandler, allow_reuse_address=False)
File "/var/lib/jenkins/shiningpanda/jobs/ddc1aed1/virtualenvs/d41d8cd9/lib/python3.7/site-packages/django/core/servers/basehttp.py", line 67, in __init__
super().__init__(*args, **kwargs)
File "/usr/lib/python3.7/socketserver.py", line 452, in __init__
self.server_bind()
File "/usr/lib/python3.7/wsgiref/simple_server.py", line 50, in server_bind
HTTPServer.server_bind(self)
File "/usr/lib/python3.7/http/server.py", line 137, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/lib/python3.7/socketserver.py", line 466, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 99] Cannot assign requested address
After hardcoded host
Traceback (most recent call last):
File "/var/lib/jenkins/workspace/Superlists/functional_tests/base.py", line 47, in setUp
self.browser = webdriver.Firefox()
File "/var/lib/jenkins/shiningpanda/jobs/ddc1aed1/virtualenvs/d41d8cd9/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 164, in __init__
self.service.start()
File "/var/lib/jenkins/shiningpanda/jobs/ddc1aed1/virtualenvs/d41d8cd9/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 100, in start
self.assert_process_still_running()
File "/var/lib/jenkins/shiningpanda/jobs/ddc1aed1/virtualenvs/d41d8cd9/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 113, in assert_process_still_running
% (self.path, return_code)
selenium.common.exceptions.WebDriverException: Message: Service geckodriver unexpectedly exited. Status code was: 69
geckodriver.log w/ hardcoded host ip in LiveTestServer
eckodriver: error: Address not available (os error 99)
geckodriver 0.27.0 (7b8c4f32cdde 2020-07-28 18:16 +0000)
WebDriver implementation for Firefox
USAGE:
geckodriver [FLAGS] [OPTIONS]
[...]
Hopefully the tracebacks above are not too confusing. Ultmiately what i did notice was the when im in webserver 2, i access the Django testcases.py module that has LiverServerThread and hardcode host=0.0.0.0 instead of host=localhost (1st traceback). The connection is then made although then the problem lies with geckodriver and the same thing (2nd traceback). I need to hard code ip 0.0.0.0 to be able to establish a connection, but then geckodriver is just listening, which i am assuming, at a completely different location (no error.log shown here).
So 1st id like to at least be able to make a connection to run the LiveServerThread class properly. Then try and resolve the issue with geckodriver. I also was not sure if the type of servers im running on poses as the problem.

wfastcgi fails import cx_Oracle, but `python -c "import cx_Oracle" succeeds

I have IIS setup with fastCGI, serving a flask app. So far so good. Next I whish to add some database connectivity, so I add the line import cx_Oracle to my app. Now this error is thrown:
Error occurred while reading WSGI handler:
Traceback (most recent call last):
File "D:\Anaconda2\lib\site-packages\wfastcgi.py", line 791, in main
env, handler = read_wsgi_handler(response.physical_path)
File "D:\Anaconda2\lib\site-packages\wfastcgi.py", line 633, in read_wsgi_handler
handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
File "D:\Anaconda2\lib\site-packages\wfastcgi.py", line 616, in get_wsgi_handler
raise ValueError('"%s" could not be imported%s' % (handler_name, last_tb))
ValueError: "Bloomberg_server.app" could not be imported:
Traceback (most recent call last):
File "D:\Anaconda2\lib\site-packages\wfastcgi.py", line 600, in get_wsgi_handler
handler = __import__(module_name, fromlist=[name_list[0][0]])
File "D:\website\init__.py", line 6, in import cx_Oracle
ImportError: DLL load failed: The specified module could not be found. StdOut: StdErr:
As the title suggests I fail to reproduce the issue in a controlled environment. The very same import statement works fine in the conda environment and moreover, I can run the Flask debug server just fine with pages that rely on a database connection.
I am at loss. Who has a clue what's going on here? The path/oracle_home variables are pointing to the instant client and I have only one python environment installed.
I am too embarrased to admit how long this has taken me, but I've found the answer.
FastCGI's core business is keeping subprocesses alive so that subsequent calls to the server do not require booting a Python environment. In other words, after installing a python package it is advised to reboot. I solved my first question on SO by rebooting..
The answer to this question got me thinking in the right direction.

Debugging in PyCharm with using Vagrant VM connection - cannot import name to_string

I've encountered an issue where I cannot debug in PyCharm connecting through Vagrant to the VM. This has worked fine for projects up until now. The VM image was recently updated so I'm wondering if that has had an effect. I can run programs in "run" mode, just not debug mode. Here's the error I get
Traceback (most recent call last):
File "/home/vagrant/.pycharm_helpers/pydev/pydevd.py", line 25, in <module>
from _pydevd_bundle import pydevd_vars
File "/home/vagrant/.pycharm_helpers/pydev/_pydevd_bundle/pydevd_vars.py", line 21, in <module>
from _pydevd_bundle.pydevd_utils import to_string
ImportError: cannot import name to_string
Here are the different commands used to run the programs - the first one is debug mode, the second is normal mode
ssh://vagrant#127.0.0.1:2222/usr/bin/python -u /home/vagrant/.pycharm_helpers/pydev/pydevd.py --multiproc --qt-support --client '0.0.0.0' --port 32807 --file /home/vagrant/.pycharm_helpers/pycharm/utrunner.py /vagrant/project/project_test.py::ProjectTest true
ssh://vagrant#127.0.0.1:2222/usr/bin/python -u /home/vagrant/.pycharm_helpers/pycharm/utrunner.py /vagrant/project/project_test.py::ProjectTest true
Any ideas what why to_string is found in normal mode but not debug?
The solution was to delete the /home/vagrant/.pycharm_helpers directory and restart PyCharm and vagrant to regenerate the directory.

Socketerror on port 80

I'm having trouble creating a working executable using PyInstaller. I've been able to successfully create a simple web server using Python and Bottle - but when I create my executable with PyInstaller and click it - I get the following output:
Alans-MBP:~ alanscarpa$ /Users/alanscarpa/Desktop/testSite/dist/testing ; exit;
Bottle v0.13-dev server starting up (using WSGIRefServer())...
Listening on http://0.0.0.0:80/
Hit Ctrl-C to quit.
Traceback (most recent call last):
File "<string>", line 10, in <module>
File "/Users/alanscarpa/Desktop/testSite/build/testing/out00-PYZ.pyz/bottle", line 3093, in run
File "/Users/alanscarpa/Desktop/testSite/build/testing/out00-PYZ.pyz/bottle", line 2692, in run
File "/Users/alanscarpa/Desktop/testSite/build/testing/out00-PYZ.pyz/wsgiref.simple_server", line 151, in make_server
File "/Users/alanscarpa/Desktop/testSite/build/testing/out00-PYZ.pyz/SocketServer", line 420, in __init__
File "/Users/alanscarpa/Desktop/testSite/build/testing/out00-PYZ.pyz/wsgiref.simple_server", line 48, in server_bind
File "/Users/alanscarpa/Desktop/testSite/build/testing/out00-PYZ.pyz/BaseHTTPServer", line 108, in server_bind
File "/Users/alanscarpa/Desktop/testSite/build/testing/out00-PYZ.pyz/SocketServer", line 434, in server_bind
File "/Users/alanscarpa/Desktop/testSite/build/testing/out00-PYZ.pyz/socket", line 224, in meth
socket.error: [Errno 13] Permission denied
logout
[Process completed]
The first 3 lines are perfect - and that's how it should look if working fine:
Bottle v0.13-dev server starting up (using WSGIRefServer())...
Listening on http://0.0.0.0:80/
Hit Ctrl-C to quit.
But then everything after that, messes up my program!
Here's my Python file called testing.py
#!/usr/bin/python
from bottle import route, run, template
#route('/testing')
def index():
return 'Hello..testing'
run(host='0.0.0.0', port=80, debug=True)
When I visit mylocalip/testing - I get webpage unavailable. But if I open up terminal and write: sudo /path/to/testing - it works perfectly. (I obviously want to be able to send the executable to someone and they just click it to run instead of having to use Terminal).
Any suggestions?
EDIT: I changed my port to 8080 and it's working! Anyone know why?
You can't listen on ports below 1024 without root privileges on most systems (at least BSD, OS X and Linux).

Unable to start AMS Acomba Connector on Windows

I am trying to get a python service to run under Windows 7 however I am unable to start it.
I have started by installing the service using
c:\amsconnector>amsconnector.py --username domain\admin --password ************ install
Installing service AMS Acomba Connector
Service installed
After that, I run
c:\amsconnector>amsconnector.py start
Starting service AMS Acomba Connector
When I check the running services list, I notice that the service is not running and when checking the event log, I am presented with this stacktrace
Python could not import the service's module
Traceback (most recent call last):
File "C:\amsconnector\amsconnector.py", line 343, in <module>
win32serviceutil.HandleCommandLine(AMSConnectorService)
File "C:\Python27\lib\site-packages\win32\lib\win32serviceutil.py", line 521, in HandleCommandLine
usage()
File "C:\Python27\lib\site-packages\win32\lib\win32serviceutil.py", line 505, in usage
sys.exit(1)
SystemExit: 1
%2: %3
The line 343 of amsconnector.py looks like this
if True or __name__ == '__main__':
win32serviceutil.HandleCommandLine(AMSConnectorService)
After checking out the source code of win32serviceutil.py, it appears that the service is just printing out the win32service usage dialog instead of actually running the python class. I am completely confused!
Thanks roeland!
I was able to fix the issue by simply removing the "True or" part of the last line leaving it like this:
if True or __name__ == '__main__':
win32serviceutil.HandleCommandLine(AMSConnectorService)

Categories