FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
programming/client_server/python/server.py at master · hacktoberfest17/programming · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
hacktoberfest17
/
programming
Public
Notifications
You must be signed in to change notification settings
Fork
765
Star
333
Code
Issues
271
Pull requests
583
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
programming
/
client_server
/
python
/
server.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (32 loc) · 1.13 KB
Breadcrumbs
programming
/
client_server
/
python
/
server.py
Copy path
File metadata and controls
39 lines (32 loc) · 1.13 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
#!/bin/python3
import
socketserver
class
tcp_handler
(
socketserver
.
BaseRequestHandler
):
'''
A simple Handler Class for tcp socket servers
'''
def
handle
(
self
):
# Note: Raw strings can't be sent
self
.
client_data
=
self
.
request
.
recv
(
10
).
strip
().
decode
(
"utf-8"
)
print
(
f"[
{
self
.
client_address
[
0
]
}
]:
{
self
.
client_data
}
"
)
self
.
request
.
sendall
(
"Pong!"
.
encode
(
"utf-8"
))
class
tcp_server
:
'''
A simple TCP server using the socketserver.TCPServer class
'''
def
__init__
(
self
,
host
,
port
):
self
.
host
=
host
self
.
port
=
port
def
start
(
self
):
try
:
# Allows us to re-use ports so we don't have to open a new port every time we run the server program
socketserver
.
TCPServer
.
allow_reuse_address
=
True
self
.
server
=
socketserver
.
TCPServer
((
self
.
host
,
self
.
port
),
tcp_handler
)
self
.
server
.
serve_forever
()
except
Exception
as
e
:
print
(
f"[Error!]:
{
e
}
"
)
finally
:
self
.
server
.
server_close
()
if
__name__
==
"__main__"
:
# Basic Example
server
=
tcp_server
(
"localhost"
,
9998
)
server
.
start
()
Back
|
FazBrowse Home
|
New Git URL