Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6902059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:45:04+00:00 2026-05-27T07:45:04+00:00

I’ve written a threaded websocket server in Python, using the lastest websocket spec and

  • 0

I’ve written a threaded websocket server in Python, using the lastest websocket spec and I’m trying to make it send a ping request to every client every x seconds. The only way I came up with to do this is overriding BaseServer.server_forever() like this:

# override BaseServer's serve_forever to send a ping request every now and then
class ModTCPServer(SocketServer.TCPServer):
    def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True):
        SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate)
        self.__is_shut_down = threading.Event()
        self.__shutdown_request = False
        
    def serve_forever(self, poll_interval=0.5):
        ###
        global PING_EVERY_SEC
        self.lastPing = int(time())
        ###
        self.__is_shut_down.clear()
        try:
            while not self.__shutdown_request:
                r, w, e = select.select([self], [], [], poll_interval)
                if self in r:
                    self._handle_request_noblock()
                ###
                now = int(time())
                if (now - self.lastPing) >= PING_EVERY_SEC:
                    self.socket.send(WebSocketPing(['0x89','0x21','0xa7','0x4b'], now)) # arbitrary key
                    self.lastPing = now
                ###
        finally:
            self.__shutdown_request = False
            self.__is_shut_down.set()

class LoginServer(SocketServer.ThreadingMixIn, ModTCPServer):
    pass

server = LoginServer(("", PORT), ApplicationHandler)
print "serving at port", PORT

server_thread = threading.Thread(target=server.serve_forever)
server_thread.daemon = True
server_thread.start()

while server_thread.isAlive():
    pass

server.shutdown()

Here is the function that constructs the Ping frame, it just puts the timestamp in the contents:

def WebSocketPing(key, timestamp=False):
    data = ['0x89','0x8a'] # 0x89 = fin,ping 0x8a = masked,len=10
    data.extend(key)
    if timestamp:
        t = str(timestamp)
    else:
        t = str(int(time()))
    for i in range(10):
        masking_byte = int(key[i%4],16)
        masked = ord(t[i])
        data.append(hex(masked ^ masking_byte))
    frame = ''
    for i in range(len(data)):
        frame += chr(int(data[i],16))
    return frame

Bad things happen when I run this

Traceback (most recent call last):
  File "LoginServer.py", line 91, in <module>
    server = LoginServer(("", PORT), ApplicationHandler)
  File "LoginServer.py", line 63, in __init__
    SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate)
  File "/usr/lib/python2.6/SocketServer.py", line 400, in __init__
    self.server_bind()
  File "/usr/lib/python2.6/SocketServer.py", line 411, in server_bind
    self.socket.bind(self.server_address)
  File "<string>", line 1, in bind
socket.error: [Errno 112] Address already in use

I assume this is down to my lack of understanding of how overriding works in Python or to a fundamentally wrong approach to this problem. Is there a better way to do this or a way to make this code work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:45:05+00:00Added an answer on May 27, 2026 at 7:45 am

    The codes does not set the properties __is_shut_down and __shutdown_request anywhere. Therefore, trying to access them fails. Create them in the constructor, like this:

    class ModTCPServer(SocketServer.TCPServer):
        def __init__(self, *args, **kwargs):
            SocketServer.TCPServer.__init__(self, *args, **kwargs)
            self.__is_shut_down = threading.Event()
            self.__shutdown_request = threading.Event()
    

    In response to the update:

    socket.error: [Errno 112] Address already in use
    

    means that another process has already bound to the specified port. On Unix, you can find that process with sudo netstat -ltpn. Alternatively, choose a different port.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.