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
e7e2ea43
Commit
e7e2ea43
authored
3 years ago
by
Lentz, Rick
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
4877e49c
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/server.py
+70
-0
70 additions, 0 deletions
...0Cohort2021JulySeptember/homework1/Lentz_Python/server.py
with
70 additions
and
0 deletions
MV3500Cohort2021JulySeptember/homework1/Lentz_Python/server.py
0 → 100644
+
70
−
0
View file @
e7e2ea43
#Adapted from https://github.com/realpython/materials/blob/master/multiconn-client.py
import
sys
import
socket
import
selectors
import
types
import
subprocess
import
time
sel
=
selectors
.
DefaultSelector
()
def
accept_wrapper
(
sock
):
conn
,
addr
=
sock
.
accept
()
# Should be ready to read
print
(
"
accepted connection from
"
,
addr
)
conn
.
setblocking
(
False
)
data
=
types
.
SimpleNamespace
(
addr
=
addr
,
inb
=
b
""
,
outb
=
b
""
)
events
=
selectors
.
EVENT_READ
|
selectors
.
EVENT_WRITE
sel
.
register
(
conn
,
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
if
recv_data
:
data
.
outb
+=
recv_data
else
:
print
(
"
closing connection to
"
,
data
.
addr
)
sel
.
unregister
(
sock
)
sock
.
close
()
if
mask
&
selectors
.
EVENT_WRITE
:
if
data
.
outb
:
fn
=
data
.
outb
.
decode
(
'
ascii
'
)
+
'
.x3d
'
print
(
"
getting
"
+
fn
)
subprocess
.
run
([
"
wget
"
,
"
https://nps.edu/x3d_repository/
"
+
fn
,
"
-O
"
,
"
x3d.x3d
"
])
subprocess
.
run
([
"
/castle_game_engine/bin/view3dscene
"
,
"
--screenshot
"
,
"
0
"
,
"
screenshot.png
"
,
"
x3d.x3d
"
])
png_bytes
=
open
(
'
screenshot.png
'
,
'
rb
'
).
read
()
print
(
"
sending png of length
"
+
str
(
len
(
png_bytes
))
+
'
to
'
,
data
.
addr
)
sent
=
sock
.
send
(
png_bytes
)
# Should be ready to write
data
.
outb
=
png_bytes
[
sent
:]
#time.sleep(5)
#subprocess.run(["rm", "screenshot.png","x3d.x3d"])
if
len
(
sys
.
argv
)
!=
3
:
print
(
"
usage:
"
,
sys
.
argv
[
0
],
"
<host> <port>
"
)
sys
.
exit
(
1
)
host
,
port
=
sys
.
argv
[
1
],
int
(
sys
.
argv
[
2
])
lsock
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
lsock
.
bind
((
host
,
port
))
lsock
.
listen
()
print
(
"
listening on
"
,
(
host
,
port
))
lsock
.
setblocking
(
False
)
sel
.
register
(
lsock
,
selectors
.
EVENT_READ
,
data
=
None
)
try
:
while
True
:
events
=
sel
.
select
(
timeout
=
None
)
for
key
,
mask
in
events
:
if
key
.
data
is
None
:
accept_wrapper
(
key
.
fileobj
)
else
:
service_connection
(
key
,
mask
)
except
KeyboardInterrupt
:
print
(
"
caught keyboard interrupt, exiting
"
)
finally
:
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