forked from furas/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
101 lines (60 loc) · 2.53 KB
/
Copy pathserver.py
File metadata and controls
101 lines (60 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
import struct
import socket
import sys
# --- constants ---
HOST = '' # local address IP (not external address IP)
# '0.0.0.0' or '' - conection on all NICs (Network Interface Card),
# '127.0.0.1' or 'localhost' - local conection only (can't connect from remote computer)
# 'local_IP' - connection only on one NIC which has this IP
PORT = 8000 # local port (not external port)
try:
# --- create socket ---
print('[DEBUG] create socket')
#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s = socket.socket() # default value is (socket.AF_INET, socket.SOCK_STREAM) so you don't have to use it
# --- options ---
# solution for "[Error 89] Address already in use". Use before bind()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# --- assign socket to local IP (local NIC) ---
print('[DEBUG] bind:', (HOST, PORT))
s.bind((HOST, PORT)) # one tuple (HOST, PORT), not two arguments
# --- set size of queue ---
print('[DEBUG] listen')
s.listen(1) # number of clients waiting in queue for "accept".
# If queue is full then client can't connect.
while True:
# --- accept client ---
# accept client and create new socket `conn` (with different port) for this client only
# and server will can use `s` to accept other clients (if you will use threading)
print('[DEBUG] accept ... waiting')
conn, addr = s.accept() # socket, address
print('[DEBUG] addr:', addr)
# --- receive data ---
print('[DEBUG] receive')
# receive length as 4-bytes
length = conn.recv(4)
print('[DEBUG] length as 4 bytes:', length)
# convert length from 4-bytes to int
length = struct.unpack('!i', length)[0] # unpack returns tuple with one element
print('[DEBUG] length:', length)
# receive data as bytes using length
data = conn.recv(length)
print('[DEBUG] data:', data)
# convert bytes to text
text = data.decode('utf-8')
print('[DEBUG] text:', text)
# --- close connection ---
# first close `conn`, later `s`
print('[DEBUG] close conn')
conn.close()
except Exception as ex:
print(ex)
except KeyboardInterrupt as ex:
print(ex)
except:
print(sys.exc_info())
finally:
# --- close socket ---
print('[DEBUG] close socket')
s.close()