Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tftp_etape3
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
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
pierre.johner
tftp_etape3
Commits
ca1adcb0
There was a problem fetching the pipeline summary.
Commit
ca1adcb0
authored
8 years ago
by
pierre.johner
Browse files
Options
Downloads
Patches
Plain Diff
Upload new file
parent
93af07b8
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Pipeline
#
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
server.py
+181
-0
181 additions, 0 deletions
server.py
with
181 additions
and
0 deletions
server.py
0 → 100644
+
181
−
0
View file @
ca1adcb0
#Author Rochat Johner Ares
import
sys
from
socket
import
*
import
file_class
import
simu_perte_class
import
struct
import
md5
#Check if the arg are valide
def
controle
(
argv
):
flag
=
True
if
not
argv
[
1
].
isdigit
():
print
(
"
Numero Port Invalide
"
)
flag
=
False
if
not
argv
[
2
].
isdigit
():
print
(
"
Taux de Perte Invalide
"
)
flag
=
False
return
flag
#State Machine
class
Statemachine
:
def
__init__
(
self
,
canal
):
self
.
currentstate
=
0
self
.
timeOut
=
0
self
.
counter
=
0
self
.
canal
=
canal
self
.
counter
=
1
print
(
"
Start Server
"
)
state
=
{
0
:
self
.
state0
,
1
:
self
.
state1
,
2
:
self
.
state2
,
3
:
self
.
state3
,
4
:
self
.
state4
,
}
#Choose the state
while
True
:
state
[
self
.
currentstate
]()
#Generate the first packet (DATA OR ACK)
def
state0
(
self
):
print
(
"
Start state 0
"
)
self
.
counter
=
1
self
.
file
=
file_class
.
File
(
""
)
self
.
file
.
RRQ
()
s
=
socket
(
AF_INET
,
SOCK_DGRAM
)
#Bind to all address
s
.
bind
((
"
0.0.0.0
"
,
69
))
self
.
canal
.
setSocket
(
s
)
d
=
self
.
file
.
getPacket
().
receive
(
self
.
canal
)
s
=
socket
(
AF_INET
,
SOCK_DGRAM
)
#Bind to all address
s
.
bind
((
"
0.0.0.0
"
,
50001
))
s
.
settimeout
(
5.0
)
self
.
canal
.
setSocket
(
s
)
d
=
struct
.
unpack
(
"
>h
"
+
str
(
len
(
d
)
-
9
)
+
"
s?5s?
"
,
d
)
self
.
file
=
file_class
.
File
(
d
[
1
])
if
d
[
0
]
==
1
:
self
.
file
.
WRQ
()
self
.
file
.
data
.
seek
(
0
)
x
=
self
.
file
.
data
.
read
(
512
)
self
.
file
.
Data
(
1
,
x
)
packet
=
self
.
file
.
getPacket
()
packet
.
send
(
self
.
canal
)
if
len
(
x
)
<
512
:
self
.
currentstate
=
4
else
:
self
.
currentstate
=
2
else
:
self
.
file
.
ACK
(
0
)
packet
=
self
.
file
.
getPacket
()
packet
.
send
(
self
.
canal
)
self
.
currentstate
=
1
print
(
"
End state 0
"
)
#State WRQ
def
state1
(
self
):
print
(
"
Start state 1
"
)
try
:
d
=
self
.
file
.
getPacket
().
receive
(
self
.
canal
);
self
.
timeOut
=
0
d
=
struct
.
unpack
(
"
>hh
"
+
str
(
len
(
d
)
-
4
)
+
"
s
"
,
d
);
if
d
[
0
]
==
5
:
self
.
currentstate
=
3
else
:
self
.
file
.
ACK
(
d
[
1
])
packet
=
self
.
file
.
getPacket
()
packet
.
send
(
self
.
canal
)
if
d
[
1
]
==
self
.
counter
:
self
.
file
.
receivePacket
(
d
[
2
])
self
.
counter
+=
1
if
len
(
d
[
2
])
<
512
:
self
.
currentstate
=
0
m
=
md5
.
new
()
m
.
update
(
self
.
file
.
data
)
result
=
m
.
hexdigest
()
print
(
result
)
except
timeout
:
packet
=
self
.
file
.
getPacket
()
packet
.
send
(
self
.
canal
)
self
.
timeOut
+=
1
print
(
"
timeOut
"
)
if
self
.
timeOut
>
4
:
self
.
timeOut
=
0
self
.
currentstate
=
3
print
(
"
End state 1
"
)
#State RRD
def
state2
(
self
):
print
(
"
Start state 2
"
)
try
:
d
=
self
.
file
.
getPacket
().
receive
(
self
.
canal
);
self
.
timeOut
=
0
d
=
struct
.
unpack
(
"
>hh
"
,
d
);
self
.
file
.
data
.
seek
((
d
[
1
])
*
512
)
x
=
self
.
file
.
data
.
read
(
512
)
self
.
file
.
Data
(
d
[
1
]
+
1
,
x
)
packet
=
self
.
file
.
getPacket
()
packet
.
send
(
self
.
canal
)
if
len
(
x
)
<
512
:
self
.
currentstate
=
4
except
timeout
:
packet
=
self
.
file
.
getPacket
()
packet
.
send
(
self
.
canal
)
self
.
timeOut
+=
1
print
(
"
timeOut
"
)
if
self
.
timeOut
>
4
:
self
.
timeOut
=
0
self
.
currentstate
=
3
print
(
"
End state 2
"
)
#Error
def
state3
(
self
):
print
(
"
Start state 3
"
)
print
(
"
Error TimeOut or FileNotFound
"
)
self
.
currentstate
=
0
print
(
"
End state 3
"
)
#Last data for get request
def
state4
(
self
):
print
(
"
Start state 4
"
)
try
:
d
=
self
.
file
.
getPacket
().
receive
(
self
.
canal
);
self
.
timeOut
=
0
self
.
currentstate
=
0
except
timeout
:
packet
=
self
.
file
.
getPacket
()
packet
.
send
(
self
.
canal
)
self
.
timeOut
+=
1
print
(
"
timeOut
"
)
if
self
.
timeOut
>
4
:
self
.
timeOut
=
0
self
.
currentstate
=
3
print
(
"
End state 4
"
)
# Entry of Progm
if
len
(
sys
.
argv
)
!=
3
:
print
(
"
Mauvais Nombre Argument : mytftps <numero_de_port> <taux_de_perte>
"
)
else
:
if
controle
(
sys
.
argv
):
canal
=
simu_perte_class
.
Canal
(
sys
.
argv
[
2
],
None
,
""
,
0
)
print
"
Wait on {}
"
.
format
(
sys
.
argv
[
1
])
stateMachine
=
Statemachine
(
canal
)
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