Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
OpenXG-Erm
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenXG
OpenXG-Erm
Commits
220d6964
Commit
220d6964
authored
Apr 12, 2021
by
Suzhi Bi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
c97894a5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
116 additions
and
0 deletions
+116
-0
user4's_code_and_picture/begin_4.py
user4's_code_and_picture/begin_4.py
+116
-0
No files found.
user4's_code_and_picture/begin_4.py
0 → 100644
View file @
220d6964
import
socket
import
os
import
sys
import
struct
import
time
def
sender
(
ip
,
time_max
,
filepath
):
try
:
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
s
.
connect
((
ip
,
8080
))
except
socket
.
error
as
msg
:
print
(
msg
)
sys
.
exit
(
1
)
# time_max = input("please enter the maximum time (seconds) allowed for picture(s): ")
if
time_max
.
isdigit
():
s
.
send
(
time_max
.
encode
(
'utf-8'
))
s
.
close
()
try
:
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
s
.
connect
((
ip
,
8080
))
except
socket
.
error
as
msg
:
print
(
msg
)
sys
.
exit
(
1
)
img_type
=
[
'.jpg'
,
'.JPG'
,
'.png'
,
'.PNG'
,
'.bmp'
,
'.BMP'
]
while
1
:
# filepath = input("please input file or folder path: ")
if
os
.
path
.
isdir
(
filepath
):
img_path_vec
=
[
os
.
path
.
join
(
filepath
,
imgpath
)
for
imgpath
in
os
.
listdir
(
filepath
)
if
os
.
path
.
splitext
(
imgpath
)[
1
]
in
img_type
]
for
img_name
in
img_path_vec
:
fileinfo_size
=
struct
.
calcsize
(
'128sq'
)
fhead
=
struct
.
pack
(
'128sq'
,
bytes
(
os
.
path
.
basename
(
img_name
).
encode
(
'utf-8'
)),
os
.
stat
(
img_name
).
st_size
)
s
.
send
(
fhead
)
print
(
'client filepath: {0}'
.
format
(
img_name
))
fp
=
open
(
img_name
,
'rb'
)
while
1
:
data
=
fp
.
read
(
1024
)
if
not
data
:
print
(
'{0} file send over...'
.
format
(
img_name
))
break
s
.
send
(
data
)
if
img_name
==
img_path_vec
[
-
1
]:
break
s
.
close
()
try
:
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
s
.
connect
((
ip
,
8080
))
except
socket
.
error
as
msg
:
print
(
msg
)
sys
.
exit
(
1
)
s
.
close
()
break
else
:
if
os
.
path
.
isfile
(
filepath
):
if
os
.
path
.
splitext
(
filepath
)[
1
]
in
img_type
:
fileinfo_size
=
struct
.
calcsize
(
'128sq'
)
fhead
=
struct
.
pack
(
'128sq'
,
bytes
(
os
.
path
.
basename
(
filepath
).
encode
(
'utf-8'
)),
os
.
stat
(
filepath
).
st_size
)
s
.
send
(
fhead
)
print
(
'client filepath: {0}'
.
format
(
filepath
))
fp
=
open
(
filepath
,
'rb'
)
while
1
:
data
=
fp
.
read
(
1024
)
if
not
data
:
print
(
'{0} file send over...'
.
format
(
filepath
))
break
s
.
send
(
data
)
else
:
print
(
'The file sent is not a picture.'
)
else
:
print
(
'Please enter the correct file or folder path.'
)
s
.
close
()
break
else
:
print
(
'Please key in numbers.'
)
def
deal_data
(
conn
,
addr
):
global
flag
flag
=
0
print
(
'Accept new connection from {0}'
.
format
(
addr
))
fileinfo_size
=
struct
.
calcsize
(
'128sq'
)
buf
=
conn
.
recv
(
fileinfo_size
)
if
buf
.
decode
(
'utf-8'
,
'ignore'
)
==
"begin"
:
flag
=
1
print
(
"Start Raspberry Pi 1."
)
conn
.
close
()
local_ip
=
'192.168.191.8'
destination_ip
=
'192.168.191.4'
time_max
=
str
(
500
)
filepath
=
"./4"
exp_time
=
20
*
60
interval_time
=
500
socket
.
setdefaulttimeout
(
10000
)
print
(
"Waiting..."
)
try
:
s
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
s
.
setsockopt
(
socket
.
SOL_SOCKET
,
socket
.
SO_REUSEADDR
,
1
)
s
.
bind
((
local_ip
,
8084
))
s
.
listen
(
10
)
except
socket
.
error
as
msg
:
print
(
msg
)
sys
.
exit
(
1
)
conn
,
addr
=
s
.
accept
()
deal_data
(
conn
,
addr
)
if
flag
==
1
:
for
i
in
range
(
int
(
exp_time
/
interval_time
)
+
1
):
sender
(
destination_ip
,
time_max
,
filepath
)
time
.
sleep
(
interval_time
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment