|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# |
| 4 | +# https://docs.python.org/3.5/library/socket.html |
| 5 | +# |
| 6 | + |
| 7 | +import socket |
| 8 | +import threading |
| 9 | +import time |
| 10 | + |
| 11 | +# --- constants --- |
| 12 | + |
| 13 | +HOST = '' # local address IP (not external address IP) |
| 14 | + |
| 15 | + # '0.0.0.0' or '' - conection on all NICs (Network Interface Card), |
| 16 | + # '127.0.0.1' or 'localhost' - local conection only (can't connect from remote computer) |
| 17 | + # 'local_IP' - connection only on one NIC which has this IP |
| 18 | + |
| 19 | +PORT = 8000 # local port (not external port) |
| 20 | + |
| 21 | + |
| 22 | +# --- functions --- |
| 23 | + |
| 24 | +def handle_client(conn, addr): |
| 25 | + |
| 26 | + try: |
| 27 | + while True: |
| 28 | + # --- receive/send data --- |
| 29 | + |
| 30 | + # if client first `send()` and next `recv()` |
| 31 | + # then server have to first `recv`() and next `send()` |
| 32 | + |
| 33 | + # if both will `recv()` at the same time then all will hang |
| 34 | + # because both will wait for data and nobody will `send()` |
| 35 | + |
| 36 | + # if you don't use native characters |
| 37 | + # then you can use 'ascii' instead of 'utf-8' |
| 38 | + |
| 39 | + now = int(time.time()) |
| 40 | + |
| 41 | + # receiving |
| 42 | + |
| 43 | + data = conn.recv(1024) |
| 44 | + text = data.decode('utf-8') # decode bytes to string |
| 45 | + |
| 46 | + print('[{}][{}] recv: {}'.format(addr, now, text)) |
| 47 | + |
| 48 | + # sending |
| 49 | + |
| 50 | + text = 'Thank you [{}]'.format(now) |
| 51 | + data = text.encode('utf-8') # encode string to bytes |
| 52 | + conn.send(data) |
| 53 | + |
| 54 | + print('[{}]][{}] send: {}'.format(addr, now, text)) |
| 55 | + |
| 56 | + except Exception as e: |
| 57 | + print('[DEBUG] exception:', e) |
| 58 | + |
| 59 | +# --- create socket --- |
| 60 | + |
| 61 | +print('[DEBUG] create socket') |
| 62 | + |
| 63 | +#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 64 | +s = socket.socket() # default value is (socket.AF_INET, socket.SOCK_STREAM) |
| 65 | + # so you don't have to use it in socket() |
| 66 | + |
| 67 | +# --- options --- |
| 68 | + |
| 69 | +print('[DEBUG] set options') |
| 70 | + |
| 71 | +# solution for "[Error 89] Address already in use". Use before bind() |
| 72 | +s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 73 | + |
| 74 | +# --- assign socket to local IP (local NIC) --- |
| 75 | + |
| 76 | +print('[DEBUG] bind:', (HOST, PORT)) |
| 77 | + |
| 78 | +s.bind((HOST, PORT)) # one tuple (HOST, PORT), not two arguments |
| 79 | + |
| 80 | +# --- set size of queue --- |
| 81 | + |
| 82 | +print('[DEBUG] listen') |
| 83 | + |
| 84 | +s.listen(1) # number of clients waiting in queue for "accept". |
| 85 | + # If queue is full then client can't connect. |
| 86 | + |
| 87 | +while True: |
| 88 | + # --- accept client --- |
| 89 | + |
| 90 | + # accept client and create new socket `conn` (with different port) for this client only |
| 91 | + # and server will can use `s` to accept other clients (if you will use threading) |
| 92 | + |
| 93 | + print('[DEBUG] accept ... waiting') |
| 94 | + |
| 95 | + conn, addr = s.accept() |
| 96 | + |
| 97 | + print('[DEBUG] addr:', addr) |
| 98 | + |
| 99 | + # --- run thread --- |
| 100 | + |
| 101 | + t = threading.Thread(target=handle_client, args=(conn, addr)) |
| 102 | + t.start() |
| 103 | + |
| 104 | +# --- close all sockets --- |
| 105 | + |
| 106 | +# alway first close `conn`, next close `s` |
| 107 | + |
| 108 | +print('[DEBUG] close socket(s)') |
| 109 | + |
| 110 | +conn.close() |
| 111 | +s.close() |
0 commit comments