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 6777009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:05:49+00:00 2026-05-26T16:05:49+00:00

How is Unix socket credential passing accomplished in Python?

  • 0

How is Unix socket credential passing accomplished in Python?

  • 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-26T16:05:50+00:00Added an answer on May 26, 2026 at 4:05 pm

    Internet searches on this topic came up with surprisingly few results. I figured I’d post the question and answer here for others interested in this topic.

    The following client and server applications demonstrate how to accomplish this on Linux with the standard python interpreter. No extensions are required but, due to the use of embedded constants, the code is Linux-specific.

    Server:

    #!/usr/bin/env python
    
    import struct
    from socket import socket, AF_UNIX, SOCK_STREAM, SOL_SOCKET
    
    SO_PEERCRED = 17 # Pulled from /usr/include/asm-generic/socket.h
    
    s = socket(AF_UNIX, SOCK_STREAM)
    
    s.bind('/tmp/pass_cred')
    s.listen(1)
    
    conn, addr = s.accept()
    
    creds = conn.getsockopt(SOL_SOCKET, SO_PEERCRED, struct.calcsize('3i'))
    
    pid, uid, gid = struct.unpack('3i',creds)
    
    print 'pid: %d, uid: %d, gid %d' % (pid, uid, gid)
    

    Client:

    #!/usr/bin/env python
    
    from socket import socket, AF_UNIX, SOCK_STREAM, SOL_SOCKET
    
    SO_PASSCRED = 16 # Pulled from /usr/include/asm-generic/socket.h
    
    s = socket(AF_UNIX, SOCK_STREAM)
    
    s.setsockopt(SOL_SOCKET, SO_PASSCRED, 1)
    
    s.connect('/tmp/pass_cred')
    
    s.close()
    

    Unfortunately, the SO_PEERCRED and SO_PASSCRED constants are not exported by python’s socket module so they must be entered by hand. Although these value are unlikely to change it is possible. This should be considered by any applications using this approach.

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

Sidebar

Related Questions

Using UNIX socket APIs on Linux, is there any way to guarantee that I
I have a class which I'm serialising to send over a unix socket and
I wonder if the UNIX domain socket connections with postgresql are faster then tcp
When using socket in the UNIX domain, it is advisable to use path name
In Socket Programming, how will a Unix thread receive a Socket CLOSE event from
I'm trying to connect, read and write from a UNIX socket in Ruby. It
I'm playing around with a webserver, using a unix socket and sendmsg / recvmsg
Can node.js listen on UNIX socket? I did not find any documentation regarding this.
I am trying to connect to unix domain socket created by another process. here
I'm a newbie in UNIX programming. Normally, when we use local socket to communicate,

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.