Attachment 'unix_socket_server.py'

Download

   1 import socket
   2 import os
   3 
   4 serveraddress="/tmp/square_server"
   5 sock=socket.socket(socket.AF_UNIX,socket.SOCK_STREAM)
   6 try:
   7         os.remove(serveraddress)
   8 except OSError:
   9         pass
  10 sock.bind(serveraddress)
  11 # 5 klientov moze cakat vo fronte
  12 sock.listen(5)
  13 while True:
  14         # sock_client vrati socket pripojeny na druhej strane na klienta
  15         (sock_client,address)=sock.accept()
  16         print "server: pripojenie od klienta"
  17         # Vybavujeme teraz jedneho klienta po druhom,
  18         # aby sme si zjednodusili zivot.
  19         # Klasika je teraz spravit fork() a vybavovat klientov
  20         # sucasne v child procesoch.
  21         input=sock_client.recv(1024)
  22         print "server: klient poslal", input
  23         output="%d\n" % (int(input)*int(input))
  24         print "server: vraciam", output
  25         sock_client.send(output)
  26         print "server: vybavene, zatvaram socket"
  27         sock_client.close()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.