Merge PR #160 into dev-1.2.x
Michael Teo
3 years ago
15 | 15 | Example |
16 | 16 | ------- |
17 | 17 | |
18 | The following code snippet illustrates file retrieval.:: | |
18 | The following code snippet illustrates file retrieval with Python 2.:: | |
19 | 19 | |
20 | 20 | # -*- coding: utf-8 -*- |
21 | 21 | import urllib2 |
33 | 33 | # Process fh2 like a file-like object and then close it. |
34 | 34 | fh2.close() |
35 | 35 | |
36 | The following code snippet illustrates file upload. You need to provide a file-like object for the *data* parameter in the *open()* method:: | |
36 | The following code snippet illustrates file upload with Python 2. You need to provide a file-like object for the *data* parameter in the *open()* method:: | |
37 | 37 | |
38 | 38 | import urllib2 |
39 | 39 | from smb.SMBHandler import SMBHandler |
45 | 45 | |
46 | 46 | # Reading from fh will only return an empty string |
47 | 47 | fh.close() |
48 | ||
49 | ||
50 | The following code snippet illustrates file retrieval with Python 3.:: | |
51 | ||
52 | import urllib | |
53 | from smb.SMBHandler import SMBHandler | |
54 | ||
55 | director = urllib.request.build_opener(SMBHandler) | |
56 | fh = director.open('smb://myuserID:[email protected]/sharedfolder/rfc1001.txt') | |
57 | ||
58 | # Process fh like a file-like object and then close it. | |
59 | fh.close() | |
60 | ||
61 | # For paths/files with unicode characters, simply pass in the URL as an unicode string | |
62 | fh2 = director.open(u'smb://myuserID:[email protected]/sharedfolder/测试文件夹/垃圾文件.dat') | |
63 | ||
64 | # Process fh2 like a file-like object and then close it. | |
65 | fh2.close() | |
66 | ||
67 | The following code snippet illustrates file upload with Python 3. You need to provide a file-like object for the *data* parameter in the *open()* method:: | |
68 | ||
69 | import urllib | |
70 | from smb.SMBHandler import SMBHandler | |
71 | ||
72 | file_fh = open('local_file.dat', 'rb') | |
73 | ||
74 | director = urllib.request.build_opener(SMBHandler) | |
75 | fh = director.open('smb://myuserID:[email protected]/sharedfolder/upload_file.dat', data = file_fh) | |
76 | ||
77 | # Reading from fh will only return an empty string | |
78 | fh.close() |