Just like MySQL server's /tmp/mysql.sock
and client write to this file throught socket or any suggestion to share content between independent process (one update, one read) without memcached
or NoSQL server, without multithread or multiprocess.
Calling conn. close() is indeed the correct way to close the connection.
In most cases you will open a new thread or process once a connection is accepted. To close the connection, break the while loop.
The server creates a socket and binds a name to the socket, then displays the port number. The program calls listen(3SOCKET) to mark the socket as ready to accept connection requests and to initialize a queue for the requests.
The close() method of ServerSocket class is used to close this socket. Any thread currently blocked in accept() will throw a SocketException.
# Echo server program
import socket,os
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
os.remove("/tmp/socketname")
except OSError:
pass
s.bind("/tmp/socketname")
s.listen(1)
conn, addr = s.accept()
while 1:
data = conn.recv(1024)
if not data: break
conn.send(data)
conn.close()
# Echo client program
import socket
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect("/tmp/socketname")
s.send(b'Hello, world')
data = s.recv(1024)
s.close()
print('Received ' + repr(data))
Shamelessly copy-pasted from the Python mailing list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With