Created
November 16, 2012 15:12
-
-
Save quiver/4088062 to your computer and use it in GitHub Desktop.
Linux Programming Interface Listing 57-6: A simple UNIX domain datagram server in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The Linux Programming Interface Listing 57-6 | |
$ python ud_ucase_sv.py & | |
[1] 437 | |
$ python ud_ucase_cl.py hello world | |
Server received 5 bytes from /tmp/ud_ucase.452 | |
Response 1 : HELLO | |
Server received 5 bytes from /tmp/ud_ucase.452 | |
Response 2 : WORLD | |
$ python ud_ucase_cl.py 'long message' | |
Server received 10 bytes from /tmp/ud_ucase.763 | |
Response 1 : LONG MESSA | |
$ kill %1 | |
[1]+ Terminated python ud_ucase_sv.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The Linux Programming Interface Listing 57-6 | |
# Python port of sockets/ud_ucase_cl.c | |
import os | |
import socket | |
import sys | |
def main(): | |
pid = os.getpid() | |
sfd = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) | |
svaddr = '/tmp/ud_ucase' | |
claddr = '/tmp/ud_ucase.%s'%pid | |
sfd.bind(claddr) | |
for idx, arg in enumerate(sys.argv[1:]): | |
msg_len = sfd.sendto(arg, svaddr) | |
data = sfd.recvfrom(msg_len) | |
print 'Response %d : %s' % (idx + 1, data[0]) | |
os.remove(claddr) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The Linux Programming Interface Listing 57-6 | |
# Python port of sockets/ud_ucase_sv.c | |
import os | |
import socket | |
import sys | |
BUF_SIZE = 10 | |
def main(): | |
sfd = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) | |
svaddr = '/tmp/ud_ucase' | |
if os.path.exists(svaddr): | |
os.remove(svaddr) | |
sfd.bind(svaddr) | |
while True: | |
data, claddr = sfd.recvfrom(BUF_SIZE) | |
print 'Server received %d bytes from %s' % (len(data), claddr) | |
msg_len = sfd.sendto(data.upper(), claddr) | |
os.remove(svaddr) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment