FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
testcontainers-python/modules/sftp/example_basic.py at main · f18m/testcontainers-python · GitHub
f18m
/
testcontainers-python
Public
forked from
testcontainers/testcontainers-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
testcontainers-python
/
modules
/
sftp
/
example_basic.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
137 lines (113 loc) · 4.28 KB
Breadcrumbs
testcontainers-python
/
modules
/
sftp
/
example_basic.py
Copy path
File metadata and controls
137 lines (113 loc) · 4.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import
json
import
os
from
datetime
import
datetime
import
paramiko
from
testcontainers
.
sftp
import
SftpContainer
def
basic_example
():
with
SftpContainer
()
as
sftp
:
# Get connection parameters
host
=
sftp
.
get_container_host_ip
()
port
=
sftp
.
get_exposed_port
(
sftp
.
port
)
username
=
sftp
.
username
password
=
sftp
.
password
# Create SSH client
ssh
=
paramiko
.
SSHClient
()
ssh
.
set_missing_host_key_policy
(
paramiko
.
AutoAddPolicy
())
ssh
.
connect
(
host
,
port
,
username
,
password
)
print
(
"Connected to SFTP server"
)
# Create SFTP client
sftp_client
=
ssh
.
open_sftp
()
# Create test directory
test_dir
=
"/home/testuser/test_dir"
sftp_client
.
mkdir
(
test_dir
)
print
(
f"Created directory:
{
test_dir
}
"
)
# Create and upload test files
test_files
=
[
{
"name"
:
"test1.txt"
,
"content"
:
"This is test file 1"
},
{
"name"
:
"test2.txt"
,
"content"
:
"This is test file 2"
},
{
"name"
:
"test3.txt"
,
"content"
:
"This is test file 3"
},
]
for
file_info
in
test_files
:
local_path
=
f"/tmp/
{
file_info
[
'name'
]
}
"
remote_path
=
f"
{
test_dir
}
/
{
file_info
[
'name'
]
}
"
# Create local file
with
open
(
local_path
,
"w"
)
as
f
:
f
.
write
(
file_info
[
"content"
])
# Upload file
sftp_client
.
put
(
local_path
,
remote_path
)
print
(
f"Uploaded file:
{
file_info
[
'name'
]
}
"
)
# Remove local file
os
.
remove
(
local_path
)
# List directory contents
print
(
"
\n
Directory contents:"
)
for
entry
in
sftp_client
.
listdir_attr
(
test_dir
):
print
(
json
.
dumps
(
{
"filename"
:
entry
.
filename
,
"size"
:
entry
.
st_size
,
"modified"
:
datetime
.
fromtimestamp
(
entry
.
st_mtime
).
isoformat
(),
},
indent
=
2
,
)
)
# Download and read file
print
(
"
\n
Reading file contents:"
)
for
file_info
in
test_files
:
remote_path
=
f"
{
test_dir
}
/
{
file_info
[
'name'
]
}
"
local_path
=
f"/tmp/
{
file_info
[
'name'
]
}
"
# Download file
sftp_client
.
get
(
remote_path
,
local_path
)
# Read and print contents
with
open
(
local_path
)
as
f
:
content
=
f
.
read
()
print
(
f"
\n
{
file_info
[
'name'
]
}
:"
)
print
(
content
)
# Remove local file
os
.
remove
(
local_path
)
# Create nested directory
nested_dir
=
f"
{
test_dir
}
/nested"
sftp_client
.
mkdir
(
nested_dir
)
print
(
f"
\n
Created nested directory:
{
nested_dir
}
"
)
# Move file to nested directory
old_path
=
f"
{
test_dir
}
/test1.txt"
new_path
=
f"
{
nested_dir
}
/test1.txt"
sftp_client
.
rename
(
old_path
,
new_path
)
print
(
"Moved file to nested directory"
)
# List nested directory
print
(
"
\n
Nested directory contents:"
)
for
entry
in
sftp_client
.
listdir_attr
(
nested_dir
):
print
(
json
.
dumps
(
{
"filename"
:
entry
.
filename
,
"size"
:
entry
.
st_size
,
"modified"
:
datetime
.
fromtimestamp
(
entry
.
st_mtime
).
isoformat
(),
},
indent
=
2
,
)
)
# Get file attributes
print
(
"
\n
File attributes:"
)
for
file_info
in
test_files
:
remote_path
=
f"
{
test_dir
}
/
{
file_info
[
'name'
]
}
"
try
:
attrs
=
sftp_client
.
stat
(
remote_path
)
print
(
f"
\n
{
file_info
[
'name'
]
}
:"
)
print
(
json
.
dumps
(
{
"size"
:
attrs
.
st_size
,
"permissions"
:
oct
(
attrs
.
st_mode
)[
-
3
:],
"modified"
:
datetime
.
fromtimestamp
(
attrs
.
st_mtime
).
isoformat
(),
},
indent
=
2
,
)
)
except
FileNotFoundError
:
print
(
f"File not found:
{
file_info
[
'name'
]
}
"
)
# Clean up
sftp_client
.
close
()
ssh
.
close
()
if
__name__
==
"__main__"
:
basic_example
()
Back
|
FazBrowse Home
|
New Git URL