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

  • Home
  • SEARCH
  • 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 4626128
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:22:26+00:00 2026-05-22T03:22:26+00:00

I have some problems using asyncore with AF_UNIX sockets. This code import asyncore, socket,

  • 0

I have some problems using asyncore with AF_UNIX sockets. This code

import asyncore, socket, os
class testselect(asyncore.dispatcher):

    path = '/tmp/mysocket'

    def __init__(self):

        asyncore.dispatcher.__init__(self)

        self.create_socket(socket.AF_UNIX, socket.SOCK_DGRAM)
        self.bind(self.path)
        self.buffer = 'buffer'

    def handle_connect(self):

        print 'handle_connect'
        pass

    def handle_close(self):
        print 'handle_close'
        if os.path.exists(self.path)       
             os.remove(self.path)
        self.close()

    def handle_read(self):
        print 'handle_read'
        print self.recv(8192)

    def writable(self):
        print 'writable'
        return (len(self.buffer) > 0)

    def handle_write(self):
        print 'handle_write'
        sent = self.send(self.buffer)
        self.buffer = self.buffer[sent:]


    client = testselect()
    asyncore.loop()

If i execute the code

 $ python select_prova.py
 writable
 handle_connect
 handle_write
 handle_close
 $  

It quits immediatly, and doesn’t wait for read and write. If i change code to force writable() method to return always False, it wait correctly for input and i can communicate with socat like this

 $ socat readline UNIX:/tmp/mysocket

But only for reading (write logically doesn’t works because writable() returns False). Are there error in my code or I can’t manage AF_UNIX sockets with asyncore/select() ?

  • 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-22T03:22:27+00:00Added an answer on May 22, 2026 at 3:22 am

    Note As the other answer points out, when you send a datagram you need to specify the receiver. As it stands, your testselect class looks more like a client than a server.

    Review some of these asyncore examples to find a server pattern you can copy. The TimeChannel example is closer to what you want — change socket.AF_INET to socket.AF_UNIX and use a socket path for the bind address to have it use a UNIX domain socket.


    You’re setting socket.SOCK_DGRAM which usually indicates creation of a UDP INET socket. Unix domain sockets are a form of IPC. You should change it to socket.SOCK_STREAM, call self.listen([backlog]), implement handle_accept(), etc.

    If you did intend to use SOCK_DGRAM with AF_UNIX, the reason your server exits is that it is indicating writable as soon as it’s started, which causes handle_write to run, sending the packet containing 'buffer' immediately.

    If you want your server to wait until it’s received a packet before replying, set the buffer in handle_connect or handle_read:

        def __init__(self):
            ...
            self.buffer = ''
    
        def handle_connect(self):
            self.buffer = 'buffer'
    

    Now when you start your server it’ll wait until it receives a packet from socat.


    I’ve rewritten your example to work more like you indend:

    import asyncore, socket, os
    
    class testselect(asyncore.dispatcher):
    
        path = '/tmp/mysocket'
    
        def __init__(self):
            asyncore.dispatcher.__init__(self)
            self.create_socket(socket.AF_UNIX, socket.SOCK_STREAM)
            self.set_reuse_addr()
            self.bind(self.path)
            self.listen(5)
    
        def handle_accept(self):
            client = self.accept()
            if client is None:
                pass
            else:
                handler = testhandler(*client)
    
    class testhandler(asyncore.dispatcher_with_send):
    
        def __init__(self, sock, addr):
            asyncore.dispatcher_with_send.__init__(self, sock)
            self.addr = addr
            self.buffer = 'greetings'
    
        def handle_read(self):
            print self.recv(8192)
    
        def writable(self):
            return (len(self.buffer) > 0)
    
        def handle_write(self):
            self.send(self.buffer)
            self.buffer = ''
    
        def handle_close(self):
            self.close()
    
    server = testselect()
    try:
        asyncore.loop()
    finally:
        if os.path.exists(testselect.path):
            os.unlink(testselect.path)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some problems with rotating images in Java using the AffineTransform class. I
I have some problems using will_paginate and named routes. Here is some code (my
I have some problems overloading operators with template members and using make_pair: class MyArchive
i have some problems in my code... I am using html and jquery.. i
I have some problems with using queries in Hebrew. I tried this: http://sphinxsearch.com/forum/view.html?id=1431 But
Here's my problem - I have some code like this: <mx:Canvas width=300 height=300> <mx:Button
I have some problems using two dimensional array. static const int PATTERNS[20][4]; static void
I have some problems sending mails through SMTP using Spring's MailSender interface and the
hello i have some problems with my php ajax script i'm using PHP/mysql i
I'm having some problems using / understanding is_dir. (Yes, I have read the PHP

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.