Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
NetworkedGraphicsMV3500
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Savage
NetworkedGraphicsMV3500
Commits
9cad0957
Commit
9cad0957
authored
3 years ago
by
Lentz, Rick
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
e7e2ea43
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
MV3500Cohort2021JulySeptember/homework1/Lentz_Python/client.py
+80
-0
80 additions, 0 deletions
...0Cohort2021JulySeptember/homework1/Lentz_Python/client.py
with
80 additions
and
0 deletions
MV3500Cohort2021JulySeptember/homework1/Lentz_Python/client.py
0 → 100644
+
80
−
0
View file @
9cad0957
#Adapted from https://github.com/realpython/materials/blob/master/multiconn-client.py
import
sys
import
socket
import
selectors
import
types
import
contextlib
import
os
with
contextlib
.
suppress
(
FileNotFoundError
):
os
.
remove
(
'
screenshot.png
'
)
sel
=
selectors
.
DefaultSelector
()
def
start_connections
(
host
,
port
,
x3d_id
):
server_addr
=
(
host
,
port
)
print
(
"
starting connection to
"
,
server_addr
)
sock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
sock
.
setblocking
(
False
)
sock
.
connect_ex
(
server_addr
)
events
=
selectors
.
EVENT_READ
|
selectors
.
EVENT_WRITE
data
=
types
.
SimpleNamespace
(
connid
=
1
,
msg_total
=
sum
([
1
]),
recv_total
=
0
,
messages
=
[
str
(
x3d_id
).
encode
()],
outb
=
b
""
,
)
sel
.
register
(
sock
,
events
,
data
=
data
)
def
service_connection
(
key
,
mask
):
sock
=
key
.
fileobj
data
=
key
.
data
if
mask
&
selectors
.
EVENT_READ
:
recv_data
=
sock
.
recv
(
1024
)
# Should be ready to read
sock
.
settimeout
(
None
)
if
not
recv_data
:
print
(
"
closing connection
"
,
data
.
connid
)
sel
.
unregister
(
sock
)
sock
.
close
()
if
recv_data
:
with
open
(
'
screenshot.png
'
,
'
ab
'
)
as
b
:
b
.
write
(
recv_data
)
data
.
recv_total
+=
len
(
recv_data
)
print
(
"
received
"
+
str
(
data
.
recv_total
)
+
"
image bytes from connection
"
,
data
.
connid
)
if
mask
&
selectors
.
EVENT_WRITE
:
if
not
data
.
outb
and
data
.
messages
:
data
.
outb
=
data
.
messages
.
pop
(
0
)
if
data
.
outb
:
print
(
"
sending
"
,
repr
(
data
.
outb
),
"
to connection
"
,
data
.
connid
)
sent
=
sock
.
send
(
data
.
outb
)
# Should be ready to write
data
.
outb
=
data
.
outb
[
sent
:]
if
len
(
sys
.
argv
)
!=
4
:
print
(
"
This program simulates the agent communicate process where a client requests a dynamically rendered X3D asset from the host.
"
)
print
(
"
usage:
"
,
sys
.
argv
[
0
],
"
<host 192.168.86.225> <port 65500> <x3d asset number [0-14138] >
"
)
sys
.
exit
(
1
)
host
,
port
,
x3d_id
=
sys
.
argv
[
1
:
4
]
start_connections
(
host
,
int
(
port
),
int
(
x3d_id
))
try
:
while
True
:
events
=
sel
.
select
(
timeout
=
1
)
if
events
:
for
key
,
mask
in
events
:
service_connection
(
key
,
mask
)
# Check for a socket being monitored to continue.
if
not
sel
.
get_map
():
break
except
KeyboardInterrupt
:
print
(
"
caught keyboard interrupt, exiting
"
)
finally
:
sel
.
close
()
sel
.
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