FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
PythonSocketScript/PythonSocketScript/server.py at main · Adebowale-Morakinyo/PythonSocketScript · GitHub
Adebowale-Morakinyo
/
PythonSocketScript
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
PythonSocketScript
/
PythonSocketScript
/
server.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
59 lines (44 loc) · 1.45 KB
Breadcrumbs
PythonSocketScript
/
PythonSocketScript
/
server.py
Copy path
File metadata and controls
59 lines (44 loc) · 1.45 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
socket
import
threading
HEADER
=
64
PORT
=
5050
SERVER
=
socket
.
gethostbyname
(
socket
.
gethostname
())
ADDR
=
(
SERVER
,
PORT
)
FORMAT
=
'utf-8'
DISCONNECT_MESSAGE
=
'!DISCONNECT'
server
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
server
.
bind
(
ADDR
)
clients
=
set
()
clients_lock
=
threading
.
Lock
()
def
handle_client
(
conn
,
addr
):
print
(
f'[NEW CONNECTION]
{
addr
}
connected.'
+
'
\n
'
)
try
:
connected
=
True
while
connected
:
msg_length
=
conn
.
recv
(
HEADER
)
if
msg_length
:
msg_length
=
int
(
msg_length
)
msg
=
conn
.
recv
(
msg_length
).
decode
(
FORMAT
)
if
msg
==
DISCONNECT_MESSAGE
:
connected
=
False
print
(
f'[
{
addr
}
]
{
msg
}
'
)
conn
.
send
(
'Message received'
.
encode
(
FORMAT
))
with
clients_lock
:
for
c
in
clients
:
c
.
sendall
((
f'[
{
addr
}
]
{
msg
}
'
+
'
\n
'
).
encode
(
FORMAT
))
finally
:
with
clients_lock
:
clients
.
remove
(
conn
)
conn
.
close
()
def
start
():
server
.
listen
()
print
(
f'[LISTENING] Server is listening on
{
SERVER
}
'
)
while
True
:
conn
,
addr
=
server
.
accept
()
with
clients_lock
:
clients
.
add
(
conn
)
thread
=
threading
.
Thread
(
target
=
handle_client
,
args
=
(
conn
,
addr
))
thread
.
start
()
print
(
f'[ACTIVE CONNECTION]
{
threading
.
active_count
()
-
1
}
'
)
print
(
'[STARTING] server is starting...'
)
start
()
Back
|
FazBrowse Home
|
New Git URL