DoS attacks are infrastructure-based and can only be mitigated adequately by firewalls, quotas, and other network-administration implementations. Packet inspection and such is the job of your network administrator(s). However, you can check whether and how many instances of a Python program are running, aborting the current instance if the number is too high.
To do this on Windows, borrow from Bill Bell's class at the Python Cookbook:
[blockquote shade=yes]
from win32event import CreateMutex
from win32api import CloseHandle, GetLastError
from winerror import ERROR_ALREADY_EXISTS
class singleinstance:
""" Limits application to single instance """
def __init__(self):
self.mutexname = "testmutex_{D0E858DF-985E-4907-B7FB-8D732C3FC3B9}"
self.mutex = CreateMutex(None, False, self.mutexname)
self.lasterror = GetLastError()
def aleradyrunning(self):
return (self.lasterror == ERROR_ALREADY_EXISTS)
def __del__(self):
if self.mutex:
CloseHandle(self.mutex)
If you are on a Unix-based system, you are best off using the os module to access tools like ps and to process that information to determine the pids of the various processes. As mentioned above, you can then abort the current instance if too many processes are already active.
