Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Pythonprac2 0
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Кяжин Никита
Pythonprac2 0
Commits
fa99b8b1
Commit
fa99b8b1
authored
1 year ago
by
root
Browse files
Options
Downloads
Patches
Plain Diff
classwork0318
parent
97c0db71
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
20240318/0/prog.py
+65
-0
65 additions, 0 deletions
20240318/0/prog.py
with
65 additions
and
0 deletions
20240318/0/prog.py
0 → 100644
+
65
−
0
View file @
fa99b8b1
# import socket
# import sys
# host = "localhost" if len(sys.argv) < 2 else sys.argv[1]
# port = 1339 if len(sys.argv) < 3 else int(sys.argv[2])
# with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# s.bind((host, port))
# s.listen()
# conn, addr = s.accept()
# with conn:
# print('Connected by', addr)
# while data := conn.recv(1024):
# print(data)
# conn.sendall(data.swapcase())
# s.close()
# import socket
# import sys
# import multiprocessing
# def serve(conn, addr):
# with conn:
# print('Connected by', addr)
# while data := conn.recv(1024):
# conn.sendall(data.swapcase())
# host = "localhost" if len(sys.argv) < 2 else sys.argv[1]
# port = 1337 if len(sys.argv) < 3 else int(sys.argv[2])
# with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# s.bind((host, port))
# s.listen()
# while True:
# conn, addr = s.accept()
# multiprocessing.Process(target=serve, args=(conn, addr)).start()
import
socket
import
sys
def
handle_command
(
command
,
params
,
conn
):
if
command
==
"
print
"
:
conn
.
sendall
(
f
"
{
str
(
params
.
encode
())
}
\n
"
)
elif
command
==
"
info
"
:
if
params
==
"
host
"
:
conn
.
sendall
(
f
"
Host:
{
conn
.
getpeername
()[
0
]
}
\n
"
.
encode
())
elif
params
==
"
port
"
:
conn
.
sendall
(
f
"
Port:
{
conn
.
getpeername
()[
1
]
}
\n
"
.
encode
())
else
:
conn
.
sendall
(
"
Invalid info parameter. Use
'
host
'
or
'
port
'
.
\n
"
.
encode
())
else
:
conn
.
sendall
(
"
Unknown command. Try
'
print
'
or
'
info
'
.
\n
"
.
encode
())
host
=
"
localhost
"
if
len
(
sys
.
argv
)
<
2
else
sys
.
argv
[
1
]
port
=
1344
if
len
(
sys
.
argv
)
<
3
else
int
(
sys
.
argv
[
2
])
with
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
as
s
:
s
.
bind
((
host
,
port
))
s
.
listen
()
conn
,
addr
=
s
.
accept
()
with
conn
:
print
(
'
Connected by
'
,
addr
)
while
data
:
=
conn
.
recv
(
1024
).
decode
().
strip
():
if
not
data
:
break
print
(
data
)
command
,
*
params
=
data
.
split
()
handle_command
(
command
,
'
'
.
join
(
params
),
conn
)
s
.
close
()
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment