What are .sock files and how to communicate with them

我的梦境 提交于 2021-02-07 20:40:15

问题


  • What are .sock files?
  • How can I communicate with a .sock file?

Elaborating on the 2nd bullet, I understand that .sock files are for Inter-process communication. How can I 'communicate' with them? Let us say a sock file is designed to respond in a specific way (For ex: it takes the input 'time' and prints out the current time).

I prefer higher level programming languages (python) more than C/C++ . It'd also be better if someone can point me to some application (like nc maybe?) that I can use to communicate with .sock files in a quick and dirty way?

Thanks


回答1:


Sock files are socket files they are endpoints in communication pipes.

how to create socket files:

  • let uwsgi create them when interacting with servers(e.g. nginx) sudo uwsgi --ini /path/to/ini/file/ In the ini file you need to have passed a path to where you want to add the socket file .ini files will on unix sysytems live at /etc/uwsgi/sites/*.ini

  • create socket files using a high level language try python: python -c "import socket as s; sock = s.socket(s.AF_UNIX); sock.bind('/tmp/test.sock')"

  • use nc Server side have: nc -l -p 1234 Client side have: nc -l -p 1234 That way you have a open socket that can communicate. I leave this here



回答2:


Here's detailed info on working with sockets in Python

https://pymotw.com/2/socket/uds.html

You can communicate with sockets using netcat-openbsd or socat

nc -U <path_to_socket_file>

socat - UNIX-CONNECT:<path_to_socket_file>

source for the second part: https://unix.stackexchange.com/questions/26715/how-can-i-communicate-with-a-unix-domain-socket-via-the-shell-on-debian-squeeze

UPDATE: here's an example of a socket server taken from the first link

import socket
import sys
import os

server_address = './uds_socket'

# Make sure the socket does not already exist
try:
    os.unlink(server_address)
except OSError:
    if os.path.exists(server_address):
        raise

# Create a UDS socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

# Bind the socket to the port
print >>sys.stderr, 'starting up on %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()
    try:
        print >>sys.stderr, 'connection from', client_address

        # Receive the data in small chunks and retransmit it
        while True:
            data = connection.recv(16)
            print >>sys.stderr, 'received "%s"' % data
            if data:
                print >>sys.stderr, 'sending data back to the client'
                connection.sendall(data.upper())
            else:
                print >>sys.stderr, 'no more data from', client_address
                break

    finally:
        # Clean up the connection
        connection.close()

save this into a file called sock.py and run

~/Development/temp ᐅ python sock.py
starting up on ./uds_socket
waiting for a connection

then connect using socat

~/Development/temp ᐅ socat - UNIX-CONNECT:uds_socket
hello
HELLO

write something - and you'll receive the same thing but in uppercase as a reply.



来源:https://stackoverflow.com/questions/52599848/what-are-sock-files-and-how-to-communicate-with-them

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!