Advertisement
furas

Python - Socket - client, server

Nov 21st, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.12 KB | None | 0 0
  1. # --- SERVER ---
  2.  
  3. #!/usr/bin/env python3
  4.  
  5. #
  6. # https://docs.python.org/3.5/library/socket.html
  7. #
  8.  
  9. import socket
  10. #import time
  11.  
  12. # - constants -
  13.  
  14. HOST = ''   # local address IP (not external address IP)
  15.  
  16.             # '0.0.0.0' or '' - conection on all NICs (Network Interface Card),
  17.             # '127.0.0.1' or 'localhost' - local conection only (can't connect from remote computer)
  18.             # 'local_IP' - connection only on one NIC which has this IP
  19.            
  20. PORT = 8000 # local port (not external port)
  21.  
  22. # - create socket -
  23.  
  24. print('[DEBUG] create socket')
  25. #s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  26. s = socket.socket() # default value is (socket.AF_INET, socket.SOCK_STREAM)
  27.                     # so you don't have to use it in socket()
  28.  
  29. # - options -
  30.  
  31. print('[DEBUG] set options')
  32. # solution for "[Error 89] Address already in use". Use before bind()
  33. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  34.  
  35. # - assign socket to local IP (local NIC)  -
  36.  
  37. print('[DEBUG] bind:', HOST, PORT)
  38. s.bind((HOST, PORT)) # one tuple (HOST, PORT), not two arguments
  39.  
  40. # - set size of queue -
  41.  
  42. print('[DEBUG] listen')
  43. s.listen(1) # number of clients waiting in queue for "accept".
  44.             # If queue is full then client can't connect.
  45.  
  46. try:
  47.     # - for test only - you don't need it -
  48.    
  49.     # client can wait for accept()
  50.     # but can't wait for bind() and listen()
  51.  
  52.     #print('[DEBUG] sleep ... for test only (normally you don\'t need it)')    
  53.     #time.sleep(5)  # waiting for test only (normally you don\'t need it)
  54.  
  55.     # - accecpt client -
  56.      
  57.     # accept client and create new socket `conn` (with different port) for this client only
  58.     # and server will can use `s` to accept other clients (if you will use threading)
  59.        
  60.     print('[DEBUG] accept ... waiting')
  61.     conn, addr = s.accept()
  62.     print('[DEBUG] addr:', addr)
  63.  
  64.     # - recevier/send data -
  65.  
  66.     # if client first `send()` and next `recv()`
  67.     # then server have to first `recv`() and next `send()`
  68.  
  69.     # if both will `recv()` at the same time then all will hang
  70.     # because both will wait for data and nobody will `send()`
  71.  
  72.     # if don't use native characters
  73.     # then you can use 'ascii' instead of 'utf-8'
  74.  
  75.     # receiving
  76.    
  77.     print('[DEBUG] receive')
  78.     data = conn.recv(1024)
  79.     text = data.decode('utf-8') # decode bytes to string
  80.     print(text)    
  81.  
  82.     # sending
  83.    
  84.     print('[DEBUG] send')
  85.     text = 'Goodbye World of Sockets in Python'
  86.     data = text.encode('utf-8') # encode string to bytes
  87.     conn.send(data)    
  88.     print(text)
  89.  
  90. except Exception as e:
  91.     print(e)
  92.  
  93. # - close all sockets -
  94.  
  95. # alway first close `conn`, next close `s`
  96. print('[DEBUG] close socket(s)')
  97. conn.close()
  98. s.close()
  99.  
  100.  
  101.  
  102.  
  103. # --- CLIENT ---
  104.  
  105. #!/usr/bin/env python3
  106.  
  107. #
  108. # https://docs.python.org/3.5/library/socket.html
  109. #
  110.  
  111. import socket
  112.  
  113. # - constants -
  114.  
  115. HOST = ''    # (local or external) address IP of remote server
  116. PORT = 8000  # (local or external) port of remote server
  117.  
  118. # server can have local address IP - used only in local network
  119. # or external address IP - used in internet on external router
  120. # (and router redirects data to internal address IP)
  121.  
  122. # - create socket -
  123.  
  124. print('[DEBUG] create socket')
  125. #s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  126. s = socket.socket() # default value is (socket.AF_INET, socket.SOCK_STREAM)
  127.                     # so you don't have to use it in socket()
  128.  
  129. # - connect to server -
  130.  
  131. print('[DEBUG] connect:', HOST, PORT)
  132. s.connect((HOST, PORT)) # one tuple (HOST, PORT), not two arguments
  133.  
  134. # - send data -
  135.  
  136. # if don't use native characters
  137. # then you can use 'ascii' instead of 'utf-8'
  138.  
  139. print('[DEBUG] send')
  140. text = "Hello World of Sockets in Python"
  141. data = text.encode('utf-8') # encode string to bytes
  142. s.send(data)    
  143. print(text)
  144.  
  145. # - receive data -
  146.  
  147. # if don't use native characters
  148. # then you can use 'ascii' instead of 'utf-8'
  149.  
  150. print('[DEBUG] receive')
  151. data = s.recv(1024)
  152. text = data.decode('utf-8') # decode bytes to string
  153. print(text)
  154.  
  155. # - close socket -
  156.  
  157. print('[DEBUG] close socket')
  158. s.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement