Django- [WinError 10013] [duplicate] - python

I'm trying to create a custom TCP stack using Python 2.6.5 on Windows 7 to serve valid http page requests on port 80 locally. But, I've run into a snag with what seems like Windows 7 tightened up security. This code worked on Vista.
Here's my sample code:
import SocketServer
import struct
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
headerText = """HTTP/1.0 200 OK
Date: Fri, 31 Dec 1999 23:59:59 GMT
Content-Type: text/html
Content-Length: 1354"""
bodyText = "<html><body>some page</body></html>"
self.request.send(headerText + "\n" + bodyText)
if __name__ == "__main__":
HOST, PORT = "localhost", 80
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()
C:\python>python TestServer.py
Traceback (most recent call last):
File "TestServer.py", line 19, in
server = SocketServer.TCPServer((HOST, PORT),
MyTCPHandler) File
"C:\Python26\lib\SocketServer.py",
line 400, in init
self.server_bind() File "C:\Python26\lib\SocketServer.py",
line 411, in server_bind
self.socket.bind(self.server_address)
File "", line 1, in bind
socket.error: [Errno 10013] An attempt
was made to access a socket in a way
forbidden by its access permissions
How exactly do I get this to work on Windows 7?
[Edit on 5/5/2010 # 2344 PDT] This answer explains that the error is caused by the need for elevated / superuser privileges when accessing ports lower than 1024. I'm going to try using a higher port number to see if that works. However, I still would like to know why my local admin account can't access port 80.

I just encountered the same issue, my system is Win7. just use the command on terminal like: netstat -na|findstr port, you will see the port has been used. So if you want to start the server without this message, you can change other port that not been used.

Try to run the server at a different port. Worked for me:
python manage.py runserver 127.0.0.1:7000
Explanation:
as mentioned on Django documentation:
If you run this script as a user with normal privileges (recommended), you might not have access to start a port on a low port number. Low port numbers are reserved for the superuser (root).
This server uses the WSGI application object specified by the WSGI_APPLICATION setting.
DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.)

On Windows Vista/7, with UAC, administrator accounts run programs in unprivileged mode by default.
Programs must prompt for administrator access before they run as administrator, with the ever-so-familiar UAC dialog. Since Python scripts aren't directly executable, there's no "Run as Administrator" context menu option.
It's possible to use ctypes.windll.shell32.IsUserAnAdmin() to detect whether the script has admin access, and ShellExecuteEx with the 'runas' verb on python.exe, with sys.argv[0] as a parameter to prompt the UAC dialog if needed.

For me it was complaining like that on Windows 7 x64 when I had another process already listening on that same port.
It is possible to see currently occupied (bound) ports by running
netstat -ban

McAfee was blocking it for me. I had to allow the program in the access protection rules
Open VirusScan
Right click on Access Protection and choose Properties
Click on "Anti-virus Standard Protection"
Select rule "Prevent mass mailing worms from sending mail" and click edit
Add the application to the Processes to exclude list and click OK
See http://www.symantec.com/connect/articles/we-are-unable-send-your-email-caused-mcafee

socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
Got this with flask :
Means that the port you're trying to bind to, is already in used by another service or process :
got a hint on this in my code developed on Eclipse / windows :
if __name__ == "__main__":
# Check the System Type before to decide to bind
# If the system is a Linux machine -:)
if platform.system() == "Linux":
app.run(host='0.0.0.0',port=5000, debug=True)
# If the system is a windows /!\ Change /!\ the /!\ Port
elif platform.system() == "Windows":
app.run(host='0.0.0.0',port=50000, debug=True)

Your local port is using by another app. I faced the same problem!
You can try the following step:
Go to command line and run it as administrator!
Type:
netstat -ano | find ":5000"
=> TCP 0.0.0.0:5000 0.0.0.0:0 LISTENING 4032
TCP [::]:5000 [::]:0 LISTENING 4032
Type:
TASKKILL /F /PID 4032
=> SUCCESS: The process with PID 4032 has been terminated.
Note: My 5000 local port was listing by PID 4032. You should give yours!

Just run on ports above 1024 , anything below is privileged, its the same deal with Linux, i use 5000 for example on wins without any UAC priv escalation.

It Seems the Port 80 is already in use. Try to Use some other Port which is not in use by any other application in your System.

I solved this on Windows 10 by editing an outbound firewall rule.
Right click "allow" on rule "Block network access for R local user accounts in SQL Server instance MSSQLSERVER"
Screenshot from Windows 10 Firewall - Outbound rules- this is what was blocking my instance

I had to allow ..\python27\python.exe in windows firewall. I don't need to do this on WinXP or Win8.

Try using a different port than 80

The main problem is port number used by another application.So you can change the port number to unused one as shown below.
In windows you can view the used port numbers used by different apps in windows task manager.
python manage.py runserver 127.0.0.1:portnumber
Ex: python manage.py runserver 127.0.0.1:8080

I just found in my case Kaspersky Internet Security 2019 Firewall was blocking net access for python. Disabling firewall working smoothly. Or adding a exception rules for python app and all file extension with *.py will also work.

Here is a simple solution to your problem..this error occurs either due to another process or bunch of processes running on the port which you are trying to run you app on.
Open cmd as an admin
This may also occure if you have setup your server as a SERVICE and the service breaks.
Give below command
netstat -ano | findstr portNo
you'll be able to see list of active task on that port.
you can close all unnecessary task by using
taskkill/pid taskNo /F
once done restart your server.

Disable Access Protection in Antivirus,
I faced same issue at last found the below logs from antivirus.
Blocked by Access Protection rule NT AUTHORITY\SYSTEM C:\WINDOWS\SYSTEM32\SVCHOST.EXE C:\PROGRAM FILES (X86)\MCAFEE\VIRUSSCAN ENTERPRISE\MCCONSOL.EXE Common Standard Protection:Prevent termination of McAfee processes Action blocked : Terminate
Blocked by port blocking rule C:\USERS\username\APPDATA\LOCAL\PROGRAMS\PYTHON\PYTHON37-32\PYTHON.EXE Anti-virus Standard Protection:Prevent mass mailing worms from sending mail

In Windows check "World Wide Web Publishing Service" if it is running, because when you install IIS it has a service that auto. starts and listens 80, 443 port.

netstat -ano | find ":80"
then TASKKILL /F /PID 4032
as rafee noor said, but this doesn't work when Port is being used by SYSTEM
Port 80 is being used by SYSTEM (PID 4), what is that?
or
NET stop HTTP
this works in above case

I found a solution to solve this problem in Python.
go to c:\python27\ directory and rigtlcick python.exe and tab to compaitbility and select the admin privilege option and apply the changes. Now you issue the command it allows to create the socket connection.

Related

What causes the Python error in running the flask example? [duplicate]

I'm trying to create a custom TCP stack using Python 2.6.5 on Windows 7 to serve valid http page requests on port 80 locally. But, I've run into a snag with what seems like Windows 7 tightened up security. This code worked on Vista.
Here's my sample code:
import SocketServer
import struct
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
headerText = """HTTP/1.0 200 OK
Date: Fri, 31 Dec 1999 23:59:59 GMT
Content-Type: text/html
Content-Length: 1354"""
bodyText = "<html><body>some page</body></html>"
self.request.send(headerText + "\n" + bodyText)
if __name__ == "__main__":
HOST, PORT = "localhost", 80
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()
C:\python>python TestServer.py
Traceback (most recent call last):
File "TestServer.py", line 19, in
server = SocketServer.TCPServer((HOST, PORT),
MyTCPHandler) File
"C:\Python26\lib\SocketServer.py",
line 400, in init
self.server_bind() File "C:\Python26\lib\SocketServer.py",
line 411, in server_bind
self.socket.bind(self.server_address)
File "", line 1, in bind
socket.error: [Errno 10013] An attempt
was made to access a socket in a way
forbidden by its access permissions
How exactly do I get this to work on Windows 7?
[Edit on 5/5/2010 # 2344 PDT] This answer explains that the error is caused by the need for elevated / superuser privileges when accessing ports lower than 1024. I'm going to try using a higher port number to see if that works. However, I still would like to know why my local admin account can't access port 80.
I just encountered the same issue, my system is Win7. just use the command on terminal like: netstat -na|findstr port, you will see the port has been used. So if you want to start the server without this message, you can change other port that not been used.
Try to run the server at a different port. Worked for me:
python manage.py runserver 127.0.0.1:7000
Explanation:
as mentioned on Django documentation:
If you run this script as a user with normal privileges (recommended), you might not have access to start a port on a low port number. Low port numbers are reserved for the superuser (root).
This server uses the WSGI application object specified by the WSGI_APPLICATION setting.
DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.)
On Windows Vista/7, with UAC, administrator accounts run programs in unprivileged mode by default.
Programs must prompt for administrator access before they run as administrator, with the ever-so-familiar UAC dialog. Since Python scripts aren't directly executable, there's no "Run as Administrator" context menu option.
It's possible to use ctypes.windll.shell32.IsUserAnAdmin() to detect whether the script has admin access, and ShellExecuteEx with the 'runas' verb on python.exe, with sys.argv[0] as a parameter to prompt the UAC dialog if needed.
For me it was complaining like that on Windows 7 x64 when I had another process already listening on that same port.
It is possible to see currently occupied (bound) ports by running
netstat -ban
McAfee was blocking it for me. I had to allow the program in the access protection rules
Open VirusScan
Right click on Access Protection and choose Properties
Click on "Anti-virus Standard Protection"
Select rule "Prevent mass mailing worms from sending mail" and click edit
Add the application to the Processes to exclude list and click OK
See http://www.symantec.com/connect/articles/we-are-unable-send-your-email-caused-mcafee
socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
Got this with flask :
Means that the port you're trying to bind to, is already in used by another service or process :
got a hint on this in my code developed on Eclipse / windows :
if __name__ == "__main__":
# Check the System Type before to decide to bind
# If the system is a Linux machine -:)
if platform.system() == "Linux":
app.run(host='0.0.0.0',port=5000, debug=True)
# If the system is a windows /!\ Change /!\ the /!\ Port
elif platform.system() == "Windows":
app.run(host='0.0.0.0',port=50000, debug=True)
Your local port is using by another app. I faced the same problem!
You can try the following step:
Go to command line and run it as administrator!
Type:
netstat -ano | find ":5000"
=> TCP 0.0.0.0:5000 0.0.0.0:0 LISTENING 4032
TCP [::]:5000 [::]:0 LISTENING 4032
Type:
TASKKILL /F /PID 4032
=> SUCCESS: The process with PID 4032 has been terminated.
Note: My 5000 local port was listing by PID 4032. You should give yours!
Just run on ports above 1024 , anything below is privileged, its the same deal with Linux, i use 5000 for example on wins without any UAC priv escalation.
It Seems the Port 80 is already in use. Try to Use some other Port which is not in use by any other application in your System.
I solved this on Windows 10 by editing an outbound firewall rule.
Right click "allow" on rule "Block network access for R local user accounts in SQL Server instance MSSQLSERVER"
Screenshot from Windows 10 Firewall - Outbound rules- this is what was blocking my instance
I had to allow ..\python27\python.exe in windows firewall. I don't need to do this on WinXP or Win8.
Try using a different port than 80
The main problem is port number used by another application.So you can change the port number to unused one as shown below.
In windows you can view the used port numbers used by different apps in windows task manager.
python manage.py runserver 127.0.0.1:portnumber
Ex: python manage.py runserver 127.0.0.1:8080
I just found in my case Kaspersky Internet Security 2019 Firewall was blocking net access for python. Disabling firewall working smoothly. Or adding a exception rules for python app and all file extension with *.py will also work.
Here is a simple solution to your problem..this error occurs either due to another process or bunch of processes running on the port which you are trying to run you app on.
Open cmd as an admin
This may also occure if you have setup your server as a SERVICE and the service breaks.
Give below command
netstat -ano | findstr portNo
you'll be able to see list of active task on that port.
you can close all unnecessary task by using
taskkill/pid taskNo /F
once done restart your server.
Disable Access Protection in Antivirus,
I faced same issue at last found the below logs from antivirus.
Blocked by Access Protection rule NT AUTHORITY\SYSTEM C:\WINDOWS\SYSTEM32\SVCHOST.EXE C:\PROGRAM FILES (X86)\MCAFEE\VIRUSSCAN ENTERPRISE\MCCONSOL.EXE Common Standard Protection:Prevent termination of McAfee processes Action blocked : Terminate
Blocked by port blocking rule C:\USERS\username\APPDATA\LOCAL\PROGRAMS\PYTHON\PYTHON37-32\PYTHON.EXE Anti-virus Standard Protection:Prevent mass mailing worms from sending mail
In Windows check "World Wide Web Publishing Service" if it is running, because when you install IIS it has a service that auto. starts and listens 80, 443 port.
netstat -ano | find ":80"
then TASKKILL /F /PID 4032
as rafee noor said, but this doesn't work when Port is being used by SYSTEM
Port 80 is being used by SYSTEM (PID 4), what is that?
or
NET stop HTTP
this works in above case
I found a solution to solve this problem in Python.
go to c:\python27\ directory and rigtlcick python.exe and tab to compaitbility and select the admin privilege option and apply the changes. Now you issue the command it allows to create the socket connection.

Can't access python server started in Windows 7 from another PC

I have two computers in the same enterprise network. The one with Windows XP (IP 192.168.101.96) and the other with Windows 7 (IP 192.168.101.98).
If I start Django project on PC with Windows XP (python manage.py runserver 192.168.101.96:8001), I can easily access it from Windows 7 PC with browser.
But the opposite way doesn't work. If I start Django project on PC with Windows 7, I can't access it from Windows XP PC.
The same situation is with command python -m SimpleHTTPServer 8001.
I tried to add a rule for external connections on TCP port 8001 in Windows firewall settings. Also I tried to add python.exe to firewall exceptions, but nothing seems to work...
It should be noted that when I start Django server and execute NETSTAT -p tcp -ano command, I can see line:
TCP 192.168.101.98:8001 0.0.0.0:0 LISTENING 6920
But nevertheless, nothing work. What can be the source of the problem?
for access to another computer server , server must be run on port 80.
you must run django server on port 80
python manage.py runserver 0.0.0.0:80
then access with type ip in the browser without post number :
http://192.168.101.98/
good luck
The problem was in ESET Endpoint Security special firewall. It's "automatic mode" blocks traffic without any promt to user. The problem can be solved by adding special rule to this firewall or changing it's main policy to "interactive mode" for example.

Can't connect to remote django server port 80

I'm having an issue running and connecting to my python django server on a windows 2012 server. To run the server I use command: python manage.py 0.0.0.0:80. This results in an error below
[Error 10013]: an attempt was made to access a socket in a way forbidden by its access permissions
I've tried running the command prompt as an administrator with no change. For reference, I am able to run the server on port 8000 but then I cannot connect to the port remotely. I have turned off firewalls as well so that is probably not the issue.
While it is preferable to run the django on port 80, I am trying to get this working on any port.
Port 80 (and many other ports) is reserved by Windows Server. Checkout this https://serverfault.com/questions/633179/cant-make-confluence-run-on-port-80-with-windows-server-2012-r2,
You may want to google "Windows Server Reserved port" for more info and a way to "unreserve" it.

How to use https for my application running on different port other than 443

My python application is running on port number 6666 on a linux machine to which I can connect using putty and I have sudo permissions to execute commands (I dont know root password)
1.If I change the port number in that application to 443 and if I run that application then getting some permission denied error at the time of socket binding
2.and if I use sudo for the above case then getting some module not found error.
If I open https://that_server_name:6666/path_to_my_appln from my localhost, then getting the error as cound not find response (as I am able to successfully run my application with port 6666, I ran the application and I tried to open that url)
or else if I open https://that_server_name:443/path_to_my_appln , then getting error as 503, service temporarily unavailable(as I am getting above mentioned errors for port number 443, so I did not start my application in backend)
My question is how to map 443 to an application running on 6666 port
In order to listen to a port below 1024 on Linux you need to have root permissions. You can
Run the program as root and secure it, for example by dropping privileges after binding to the socket.
Use a webserver (Apache, nginx, ...) to proxy the request.
Of cause there are some more solutions.
You should try to solve the problem of module not found error. That would be a good solution to your problem. If you post your module not found error that would be helpful. How are you running the Python application. Are you running it from a virtualenv?

socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions

I'm trying to create a custom TCP stack using Python 2.6.5 on Windows 7 to serve valid http page requests on port 80 locally. But, I've run into a snag with what seems like Windows 7 tightened up security. This code worked on Vista.
Here's my sample code:
import SocketServer
import struct
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
headerText = """HTTP/1.0 200 OK
Date: Fri, 31 Dec 1999 23:59:59 GMT
Content-Type: text/html
Content-Length: 1354"""
bodyText = "<html><body>some page</body></html>"
self.request.send(headerText + "\n" + bodyText)
if __name__ == "__main__":
HOST, PORT = "localhost", 80
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()
C:\python>python TestServer.py
Traceback (most recent call last):
File "TestServer.py", line 19, in
server = SocketServer.TCPServer((HOST, PORT),
MyTCPHandler) File
"C:\Python26\lib\SocketServer.py",
line 400, in init
self.server_bind() File "C:\Python26\lib\SocketServer.py",
line 411, in server_bind
self.socket.bind(self.server_address)
File "", line 1, in bind
socket.error: [Errno 10013] An attempt
was made to access a socket in a way
forbidden by its access permissions
How exactly do I get this to work on Windows 7?
[Edit on 5/5/2010 # 2344 PDT] This answer explains that the error is caused by the need for elevated / superuser privileges when accessing ports lower than 1024. I'm going to try using a higher port number to see if that works. However, I still would like to know why my local admin account can't access port 80.
I just encountered the same issue, my system is Win7. just use the command on terminal like: netstat -na|findstr port, you will see the port has been used. So if you want to start the server without this message, you can change other port that not been used.
Try to run the server at a different port. Worked for me:
python manage.py runserver 127.0.0.1:7000
Explanation:
as mentioned on Django documentation:
If you run this script as a user with normal privileges (recommended), you might not have access to start a port on a low port number. Low port numbers are reserved for the superuser (root).
This server uses the WSGI application object specified by the WSGI_APPLICATION setting.
DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.)
On Windows Vista/7, with UAC, administrator accounts run programs in unprivileged mode by default.
Programs must prompt for administrator access before they run as administrator, with the ever-so-familiar UAC dialog. Since Python scripts aren't directly executable, there's no "Run as Administrator" context menu option.
It's possible to use ctypes.windll.shell32.IsUserAnAdmin() to detect whether the script has admin access, and ShellExecuteEx with the 'runas' verb on python.exe, with sys.argv[0] as a parameter to prompt the UAC dialog if needed.
For me it was complaining like that on Windows 7 x64 when I had another process already listening on that same port.
It is possible to see currently occupied (bound) ports by running
netstat -ban
McAfee was blocking it for me. I had to allow the program in the access protection rules
Open VirusScan
Right click on Access Protection and choose Properties
Click on "Anti-virus Standard Protection"
Select rule "Prevent mass mailing worms from sending mail" and click edit
Add the application to the Processes to exclude list and click OK
See http://www.symantec.com/connect/articles/we-are-unable-send-your-email-caused-mcafee
socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
Got this with flask :
Means that the port you're trying to bind to, is already in used by another service or process :
got a hint on this in my code developed on Eclipse / windows :
if __name__ == "__main__":
# Check the System Type before to decide to bind
# If the system is a Linux machine -:)
if platform.system() == "Linux":
app.run(host='0.0.0.0',port=5000, debug=True)
# If the system is a windows /!\ Change /!\ the /!\ Port
elif platform.system() == "Windows":
app.run(host='0.0.0.0',port=50000, debug=True)
Your local port is using by another app. I faced the same problem!
You can try the following step:
Go to command line and run it as administrator!
Type:
netstat -ano | find ":5000"
=> TCP 0.0.0.0:5000 0.0.0.0:0 LISTENING 4032
TCP [::]:5000 [::]:0 LISTENING 4032
Type:
TASKKILL /F /PID 4032
=> SUCCESS: The process with PID 4032 has been terminated.
Note: My 5000 local port was listing by PID 4032. You should give yours!
Just run on ports above 1024 , anything below is privileged, its the same deal with Linux, i use 5000 for example on wins without any UAC priv escalation.
It Seems the Port 80 is already in use. Try to Use some other Port which is not in use by any other application in your System.
I solved this on Windows 10 by editing an outbound firewall rule.
Right click "allow" on rule "Block network access for R local user accounts in SQL Server instance MSSQLSERVER"
Screenshot from Windows 10 Firewall - Outbound rules- this is what was blocking my instance
I had to allow ..\python27\python.exe in windows firewall. I don't need to do this on WinXP or Win8.
Try using a different port than 80
The main problem is port number used by another application.So you can change the port number to unused one as shown below.
In windows you can view the used port numbers used by different apps in windows task manager.
python manage.py runserver 127.0.0.1:portnumber
Ex: python manage.py runserver 127.0.0.1:8080
I just found in my case Kaspersky Internet Security 2019 Firewall was blocking net access for python. Disabling firewall working smoothly. Or adding a exception rules for python app and all file extension with *.py will also work.
Here is a simple solution to your problem..this error occurs either due to another process or bunch of processes running on the port which you are trying to run you app on.
Open cmd as an admin
This may also occure if you have setup your server as a SERVICE and the service breaks.
Give below command
netstat -ano | findstr portNo
you'll be able to see list of active task on that port.
you can close all unnecessary task by using
taskkill/pid taskNo /F
once done restart your server.
Disable Access Protection in Antivirus,
I faced same issue at last found the below logs from antivirus.
Blocked by Access Protection rule NT AUTHORITY\SYSTEM C:\WINDOWS\SYSTEM32\SVCHOST.EXE C:\PROGRAM FILES (X86)\MCAFEE\VIRUSSCAN ENTERPRISE\MCCONSOL.EXE Common Standard Protection:Prevent termination of McAfee processes Action blocked : Terminate
Blocked by port blocking rule C:\USERS\username\APPDATA\LOCAL\PROGRAMS\PYTHON\PYTHON37-32\PYTHON.EXE Anti-virus Standard Protection:Prevent mass mailing worms from sending mail
In Windows check "World Wide Web Publishing Service" if it is running, because when you install IIS it has a service that auto. starts and listens 80, 443 port.
netstat -ano | find ":80"
then TASKKILL /F /PID 4032
as rafee noor said, but this doesn't work when Port is being used by SYSTEM
Port 80 is being used by SYSTEM (PID 4), what is that?
or
NET stop HTTP
this works in above case
I found a solution to solve this problem in Python.
go to c:\python27\ directory and rigtlcick python.exe and tab to compaitbility and select the admin privilege option and apply the changes. Now you issue the command it allows to create the socket connection.

Categories

Resources