re-common 10.0.22__py3-none-any.whl → 10.0.24__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (202) hide show
  1. re_common/baselibrary/__init__.py +4 -4
  2. re_common/baselibrary/baseabs/__init__.py +6 -6
  3. re_common/baselibrary/baseabs/baseabs.py +26 -26
  4. re_common/baselibrary/database/mbuilder.py +132 -132
  5. re_common/baselibrary/database/moudle.py +93 -93
  6. re_common/baselibrary/database/msqlite3.py +194 -194
  7. re_common/baselibrary/database/mysql.py +169 -169
  8. re_common/baselibrary/database/sql_factory.py +26 -26
  9. re_common/baselibrary/mthread/MThreadingRun.py +486 -486
  10. re_common/baselibrary/mthread/MThreadingRunEvent.py +349 -349
  11. re_common/baselibrary/mthread/__init__.py +2 -2
  12. re_common/baselibrary/mthread/mythreading.py +695 -695
  13. re_common/baselibrary/pakge_other/socks.py +404 -404
  14. re_common/baselibrary/readconfig/config_factory.py +18 -18
  15. re_common/baselibrary/readconfig/ini_config.py +317 -317
  16. re_common/baselibrary/readconfig/toml_config.py +49 -49
  17. re_common/baselibrary/temporary/envdata.py +36 -36
  18. re_common/baselibrary/tools/all_requests/aiohttp_request.py +118 -118
  19. re_common/baselibrary/tools/all_requests/httpx_requet.py +102 -102
  20. re_common/baselibrary/tools/all_requests/mrequest.py +412 -412
  21. re_common/baselibrary/tools/all_requests/requests_request.py +81 -81
  22. re_common/baselibrary/tools/batch_compre/bijiao_batch.py +31 -31
  23. re_common/baselibrary/tools/contrast_db3.py +123 -123
  24. re_common/baselibrary/tools/copy_file.py +39 -39
  25. re_common/baselibrary/tools/db3_2_sizedb3.py +102 -102
  26. re_common/baselibrary/tools/foreachgz.py +39 -39
  27. re_common/baselibrary/tools/get_attr.py +10 -10
  28. re_common/baselibrary/tools/image_to_pdf.py +61 -61
  29. re_common/baselibrary/tools/java_code_deal.py +139 -139
  30. re_common/baselibrary/tools/javacode.py +79 -79
  31. re_common/baselibrary/tools/mdb_db3.py +48 -48
  32. re_common/baselibrary/tools/merge_file.py +171 -171
  33. re_common/baselibrary/tools/merge_gz_file.py +165 -165
  34. re_common/baselibrary/tools/mhdfstools/down_hdfs_files.py +42 -42
  35. re_common/baselibrary/tools/mhdfstools/hdfst.py +42 -42
  36. re_common/baselibrary/tools/mhdfstools/up_hdfs_files.py +38 -38
  37. re_common/baselibrary/tools/mongo_tools.py +50 -50
  38. re_common/baselibrary/tools/move_file.py +170 -170
  39. re_common/baselibrary/tools/move_mongo/mongo_table_to_file.py +63 -63
  40. re_common/baselibrary/tools/move_mongo/move_mongo_table.py +354 -354
  41. re_common/baselibrary/tools/move_mongo/use_mttf.py +18 -18
  42. re_common/baselibrary/tools/move_mongo/use_mv.py +93 -93
  43. re_common/baselibrary/tools/mpandas/mpandasreadexcel.py +125 -125
  44. re_common/baselibrary/tools/mpandas/pandas_visualization.py +7 -7
  45. re_common/baselibrary/tools/myparsel.py +104 -104
  46. re_common/baselibrary/tools/rename_dir_file.py +37 -37
  47. re_common/baselibrary/tools/sequoiadb_utils.py +398 -398
  48. re_common/baselibrary/tools/split_line_to_many.py +25 -25
  49. re_common/baselibrary/tools/stringtodicts.py +33 -33
  50. re_common/baselibrary/tools/workwechant_bot.py +84 -84
  51. re_common/baselibrary/utils/baseaiohttp.py +296 -296
  52. re_common/baselibrary/utils/baseaiomysql.py +87 -87
  53. re_common/baselibrary/utils/baseallstep.py +191 -191
  54. re_common/baselibrary/utils/baseavro.py +19 -19
  55. re_common/baselibrary/utils/baseboto3.py +291 -291
  56. re_common/baselibrary/utils/basecsv.py +32 -32
  57. re_common/baselibrary/utils/basedict.py +133 -133
  58. re_common/baselibrary/utils/basedir.py +241 -241
  59. re_common/baselibrary/utils/baseencode.py +351 -351
  60. re_common/baselibrary/utils/baseencoding.py +28 -28
  61. re_common/baselibrary/utils/baseesdsl.py +86 -86
  62. re_common/baselibrary/utils/baseexcel.py +264 -264
  63. re_common/baselibrary/utils/baseexcept.py +109 -109
  64. re_common/baselibrary/utils/basefile.py +654 -654
  65. re_common/baselibrary/utils/baseftp.py +214 -214
  66. re_common/baselibrary/utils/basegzip.py +60 -60
  67. re_common/baselibrary/utils/basehdfs.py +135 -135
  68. re_common/baselibrary/utils/basehttpx.py +268 -268
  69. re_common/baselibrary/utils/baseip.py +87 -87
  70. re_common/baselibrary/utils/basejson.py +2 -2
  71. re_common/baselibrary/utils/baselist.py +32 -32
  72. re_common/baselibrary/utils/basemotor.py +190 -190
  73. re_common/baselibrary/utils/basemssql.py +98 -98
  74. re_common/baselibrary/utils/baseodbc.py +113 -113
  75. re_common/baselibrary/utils/basepandas.py +302 -302
  76. re_common/baselibrary/utils/basepeewee.py +11 -11
  77. re_common/baselibrary/utils/basepika.py +180 -180
  78. re_common/baselibrary/utils/basepydash.py +143 -143
  79. re_common/baselibrary/utils/basepymongo.py +230 -230
  80. re_common/baselibrary/utils/basequeue.py +22 -22
  81. re_common/baselibrary/utils/baserar.py +57 -57
  82. re_common/baselibrary/utils/baserequest.py +279 -279
  83. re_common/baselibrary/utils/baseset.py +8 -8
  84. re_common/baselibrary/utils/basesmb.py +403 -403
  85. re_common/baselibrary/utils/basestring.py +382 -382
  86. re_common/baselibrary/utils/basetime.py +320 -320
  87. re_common/baselibrary/utils/baseurl.py +121 -121
  88. re_common/baselibrary/utils/basezip.py +57 -57
  89. re_common/baselibrary/utils/core/__init__.py +7 -7
  90. re_common/baselibrary/utils/core/bottomutils.py +18 -18
  91. re_common/baselibrary/utils/core/mdeprecated.py +327 -327
  92. re_common/baselibrary/utils/core/mlamada.py +16 -16
  93. re_common/baselibrary/utils/core/msginfo.py +25 -25
  94. re_common/baselibrary/utils/core/requests_core.py +103 -103
  95. re_common/baselibrary/utils/fateadm.py +429 -429
  96. re_common/baselibrary/utils/importfun.py +123 -123
  97. re_common/baselibrary/utils/mfaker.py +57 -57
  98. re_common/baselibrary/utils/my_abc/__init__.py +3 -3
  99. re_common/baselibrary/utils/my_abc/better_abc.py +32 -32
  100. re_common/baselibrary/utils/mylogger.py +414 -414
  101. re_common/baselibrary/utils/myredisclient.py +861 -861
  102. re_common/baselibrary/utils/pipupgrade.py +21 -21
  103. re_common/baselibrary/utils/ringlist.py +85 -85
  104. re_common/baselibrary/utils/version_compare.py +36 -36
  105. re_common/baselibrary/utils/ydmhttp.py +126 -126
  106. re_common/facade/lazy_import.py +11 -11
  107. re_common/facade/loggerfacade.py +25 -25
  108. re_common/facade/mysqlfacade.py +467 -467
  109. re_common/facade/now.py +31 -31
  110. re_common/facade/sqlite3facade.py +257 -257
  111. re_common/facade/use/mq_use_facade.py +83 -83
  112. re_common/facade/use/proxy_use_facade.py +19 -19
  113. re_common/libtest/base_dict_test.py +19 -19
  114. re_common/libtest/baseavro_test.py +13 -13
  115. re_common/libtest/basefile_test.py +14 -14
  116. re_common/libtest/basemssql_test.py +77 -77
  117. re_common/libtest/baseodbc_test.py +7 -7
  118. re_common/libtest/basepandas_test.py +38 -38
  119. re_common/libtest/get_attr_test/get_attr_test_settings.py +14 -14
  120. re_common/libtest/get_attr_test/settings.py +54 -54
  121. re_common/libtest/idencode_test.py +53 -53
  122. re_common/libtest/iniconfig_test.py +35 -35
  123. re_common/libtest/ip_test.py +34 -34
  124. re_common/libtest/merge_file_test.py +20 -20
  125. re_common/libtest/mfaker_test.py +8 -8
  126. re_common/libtest/mm3_test.py +31 -31
  127. re_common/libtest/mylogger_test.py +88 -88
  128. re_common/libtest/myparsel_test.py +27 -27
  129. re_common/libtest/mysql_test.py +151 -151
  130. re_common/libtest/pymongo_test.py +21 -21
  131. re_common/libtest/split_test.py +11 -11
  132. re_common/libtest/sqlite3_merge_test.py +5 -5
  133. re_common/libtest/sqlite3_test.py +34 -34
  134. re_common/libtest/tomlconfig_test.py +30 -30
  135. re_common/libtest/use_tools_test/__init__.py +2 -2
  136. re_common/libtest/user/__init__.py +4 -4
  137. re_common/studio/__init__.py +4 -4
  138. re_common/studio/assignment_expressions.py +36 -36
  139. re_common/studio/mydash/test1.py +18 -18
  140. re_common/studio/pydashstudio/first.py +9 -9
  141. re_common/studio/streamlitstudio/first_app.py +65 -65
  142. re_common/studio/streamlitstudio/uber_pickups.py +23 -23
  143. re_common/studio/test.py +18 -18
  144. re_common/v2/baselibrary/business_utils/BusinessStringUtil.py +195 -0
  145. re_common/v2/baselibrary/business_utils/__init__.py +0 -0
  146. re_common/v2/baselibrary/business_utils/rel_tools.py +6 -0
  147. re_common/v2/baselibrary/decorators/utils.py +59 -59
  148. re_common/v2/baselibrary/s3object/baseboto3.py +230 -230
  149. re_common/v2/baselibrary/tools/WeChatRobot.py +95 -79
  150. re_common/v2/baselibrary/tools/ac_ahocorasick.py +75 -75
  151. re_common/v2/baselibrary/tools/dict_tools.py +37 -37
  152. re_common/v2/baselibrary/tools/dolphinscheduler.py +187 -187
  153. re_common/v2/baselibrary/tools/hdfs_data_processer.py +338 -338
  154. re_common/v2/baselibrary/tools/list_tools.py +65 -65
  155. re_common/v2/baselibrary/tools/search_hash_tools.py +54 -54
  156. re_common/v2/baselibrary/tools/text_matcher.py +326 -326
  157. re_common/v2/baselibrary/tools/unionfind_tools.py +60 -60
  158. re_common/v2/baselibrary/utils/BusinessStringUtil.py +196 -196
  159. re_common/v2/baselibrary/utils/author_smi.py +360 -360
  160. re_common/v2/baselibrary/utils/base_string_similarity.py +158 -158
  161. re_common/v2/baselibrary/utils/basedict.py +37 -37
  162. re_common/v2/baselibrary/utils/basehdfs.py +161 -161
  163. re_common/v2/baselibrary/utils/basepika.py +180 -180
  164. re_common/v2/baselibrary/utils/basetime.py +77 -77
  165. re_common/v2/baselibrary/utils/db.py +38 -38
  166. re_common/v2/baselibrary/utils/json_cls.py +16 -16
  167. re_common/v2/baselibrary/utils/mq.py +83 -83
  168. re_common/v2/baselibrary/utils/n_ary_expression_tree.py +243 -243
  169. re_common/v2/baselibrary/utils/string_bool.py +186 -149
  170. re_common/v2/baselibrary/utils/string_clear.py +227 -204
  171. re_common/v2/baselibrary/utils/string_smi.py +18 -18
  172. re_common/v2/baselibrary/utils/stringutils.py +213 -213
  173. re_common/vip/base_step_process.py +11 -11
  174. re_common/vip/baseencodeid.py +90 -90
  175. re_common/vip/changetaskname.py +28 -28
  176. re_common/vip/core_var.py +24 -24
  177. re_common/vip/mmh3Hash.py +89 -89
  178. re_common/vip/proxy/allproxys.py +127 -127
  179. re_common/vip/proxy/allproxys_thread.py +159 -159
  180. re_common/vip/proxy/cnki_proxy.py +153 -153
  181. re_common/vip/proxy/kuaidaili.py +87 -87
  182. re_common/vip/proxy/proxy_all.py +113 -113
  183. re_common/vip/proxy/update_kuaidaili_0.py +42 -42
  184. re_common/vip/proxy/wanfang_proxy.py +152 -152
  185. re_common/vip/proxy/wp_proxy_all.py +181 -181
  186. re_common/vip/read_rawid_to_txt.py +91 -91
  187. re_common/vip/title/__init__.py +5 -5
  188. re_common/vip/title/transform/TransformBookTitleToZt.py +125 -125
  189. re_common/vip/title/transform/TransformConferenceTitleToZt.py +139 -139
  190. re_common/vip/title/transform/TransformCstadTitleToZt.py +195 -195
  191. re_common/vip/title/transform/TransformJournalTitleToZt.py +203 -203
  192. re_common/vip/title/transform/TransformPatentTitleToZt.py +132 -132
  193. re_common/vip/title/transform/TransformRegulationTitleToZt.py +114 -114
  194. re_common/vip/title/transform/TransformStandardTitleToZt.py +135 -135
  195. re_common/vip/title/transform/TransformThesisTitleToZt.py +135 -135
  196. re_common/vip/title/transform/__init__.py +10 -10
  197. {re_common-10.0.22.dist-info → re_common-10.0.24.dist-info}/LICENSE +201 -201
  198. {re_common-10.0.22.dist-info → re_common-10.0.24.dist-info}/METADATA +16 -16
  199. re_common-10.0.24.dist-info/RECORD +230 -0
  200. {re_common-10.0.22.dist-info → re_common-10.0.24.dist-info}/WHEEL +1 -1
  201. re_common-10.0.22.dist-info/RECORD +0 -227
  202. {re_common-10.0.22.dist-info → re_common-10.0.24.dist-info}/top_level.txt +0 -0
@@ -1,404 +1,404 @@
1
- """SocksiPy - Python SOCKS module.
2
- Version 1.00
3
-
4
- Copyright 2006 Dan-Haim. All rights reserved.
5
-
6
- Redistribution and use in source and binary forms, with or without modification,
7
- are permitted provided that the following conditions are met:
8
- 1. Redistributions of source code must retain the above copyright notice, this
9
- list of conditions and the following disclaimer.
10
- 2. Redistributions in binary form must reproduce the above copyright notice,
11
- this list of conditions and the following disclaimer in the documentation
12
- and/or other materials provided with the distribution.
13
- 3. Neither the name of Dan Haim nor the names of his contributors may be used
14
- to endorse or promote products derived from this software without specific
15
- prior written permission.
16
-
17
- THIS SOFTWARE IS PROVIDED BY DAN HAIM "AS IS" AND ANY EXPRESS OR IMPLIED
18
- WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19
- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20
- EVENT SHALL DAN HAIM OR HIS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21
- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA
23
- OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
- OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMANGE.
26
-
27
-
28
- This module provides a standard socket-like interface for Python
29
- for tunneling connections through SOCKS proxies.
30
-
31
- """
32
-
33
- import socket
34
- import struct
35
-
36
- PROXY_TYPE_SOCKS4 = 1
37
- PROXY_TYPE_SOCKS5 = 2
38
- PROXY_TYPE_HTTP = 3
39
-
40
- _defaultproxy = None
41
- _orgsocket = socket.socket
42
-
43
-
44
- class ProxyError(Exception):
45
- def __init__(self, value):
46
- self.value = value
47
-
48
- def __str__(self):
49
- return repr(self.value)
50
-
51
-
52
- class GeneralProxyError(ProxyError):
53
- def __init__(self, value):
54
- self.value = value
55
-
56
- def __str__(self):
57
- return repr(self.value)
58
-
59
-
60
- class Socks5AuthError(ProxyError):
61
- def __init__(self, value):
62
- self.value = value
63
-
64
- def __str__(self):
65
- return repr(self.value)
66
-
67
-
68
- class Socks5Error(ProxyError):
69
- def __init__(self, value):
70
- self.value = value
71
-
72
- def __str__(self):
73
- return repr(self.value)
74
-
75
-
76
- class Socks4Error(ProxyError):
77
- def __init__(self, value):
78
- self.value = value
79
-
80
- def __str__(self):
81
- return repr(self.value)
82
-
83
-
84
- class HTTPError(ProxyError):
85
- def __init__(self, value):
86
- self.value = value
87
-
88
- def __str__(self):
89
- return repr(self.value)
90
-
91
-
92
- _generalerrors = ("success",
93
- "invalid data",
94
- "not connected",
95
- "not available",
96
- "bad proxy type",
97
- "bad input")
98
-
99
- _socks5errors = ("succeeded",
100
- "general SOCKS server failure",
101
- "connection not allowed by ruleset",
102
- "Network unreachable",
103
- "Host unreachable",
104
- "Connection refused",
105
- "TTL expired",
106
- "Command not supported",
107
- "Address type not supported",
108
- "Unknown error")
109
-
110
- _socks5autherrors = ("succeeded",
111
- "authentication is required",
112
- "all offered authentication methods were rejected",
113
- "unknown username or invalid password",
114
- "unknown error")
115
-
116
- _socks4errors = ("request granted",
117
- "request rejected or failed",
118
- "request rejected because SOCKS server cannot connect to identd on the client",
119
- "request rejected because the client program and identd report different user-ids",
120
- "unknown error")
121
-
122
-
123
- def setdefaultproxy(proxytype=None, addr=None, port=None, rdns=True, username=None, password=None):
124
- """setdefaultproxy(proxytype, addr[, port[, rdns[, username[, password]]]])
125
- Sets a default proxy which all further socksocket objects will use,
126
- unless explicitly changed.
127
- """
128
- global _defaultproxy
129
- _defaultproxy = (proxytype, addr, port, rdns, username, password)
130
-
131
-
132
- class socksocket(socket.socket):
133
- """socksocket([family[, type[, proto]]]) -> socket object
134
-
135
- Open a SOCKS enabled socket. The parameters are the same as
136
- those of the standard socket init. In order for SOCKS to work,
137
- you must specify family=AF_INET, type=SOCK_STREAM and proto=0.
138
- """
139
-
140
- def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, _sock=None):
141
- _orgsocket.__init__(self, family, type, proto, _sock)
142
- if _defaultproxy != None:
143
- self.__proxy = _defaultproxy
144
- else:
145
- self.__proxy = (None, None, None, None, None, None)
146
- self.__proxysockname = None
147
- self.__proxypeername = None
148
-
149
- def __recvall(self, bytes):
150
- """__recvall(bytes) -> data
151
- Receive EXACTLY the number of bytes requested from the socket.
152
- Blocks until the required number of bytes have been received.
153
- """
154
- data = ""
155
- while len(data) < bytes:
156
- data = data + self.recv(bytes - len(data))
157
- return data
158
-
159
- def setproxy(self, proxytype=None, addr=None, port=None, rdns=True, username=None, password=None):
160
- """setproxy(proxytype, addr[, port[, rdns[, username[, password]]]])
161
- Sets the proxy to be used.
162
- proxytype - The type of the proxy to be used. Three types
163
- are supported: PROXY_TYPE_SOCKS4 (including socks4a),
164
- PROXY_TYPE_SOCKS5 and PROXY_TYPE_HTTP
165
- addr - The address of the server (IP or DNS).
166
- port - The port of the server. Defaults to 1080 for SOCKS
167
- servers and 8080 for HTTP proxy servers.
168
- rdns - Should DNS queries be preformed on the remote side
169
- (rather than the local side). The default is True.
170
- Note: This has no effect with SOCKS4 servers.
171
- username - Username to authenticate with to the server.
172
- The default is no authentication.
173
- password - Password to authenticate with to the server.
174
- Only relevant when username is also provided.
175
- """
176
- self.__proxy = (proxytype, addr, port, rdns, username, password)
177
-
178
- def __negotiatesocks5(self, destaddr, destport):
179
- """__negotiatesocks5(self,destaddr,destport)
180
- Negotiates a connection through a SOCKS5 server.
181
- """
182
- # First we'll send the authentication packages we support.
183
- if (self.__proxy[4] != None) and (self.__proxy[5] != None):
184
- # The username/password details were supplied to the
185
- # setproxy method so we support the USERNAME/PASSWORD
186
- # authentication (in addition to the standard none).
187
- self.sendall("\x05\x02\x00\x02")
188
- else:
189
- # No username/password were entered, therefore we
190
- # only support connections with no authentication.
191
- self.sendall("\x05\x01\x00")
192
- # We'll receive the server's response to determine which
193
- # method was selected
194
- chosenauth = self.__recvall(2)
195
- if chosenauth[0] != "\x05":
196
- self.close()
197
- raise GeneralProxyError((1, _generalerrors[1]))
198
- # Check the chosen authentication method
199
- if chosenauth[1] == "\x00":
200
- # No authentication is required
201
- pass
202
- elif chosenauth[1] == "\x02":
203
- # Okay, we need to perform a basic username/password
204
- # authentication.
205
- self.sendall(
206
- "\x01" + chr(len(self.__proxy[4])) + self.__proxy[4] + chr(len(self.proxy[5])) + self.__proxy[5])
207
- authstat = self.__recvall(2)
208
- if authstat[0] != "\x01":
209
- # Bad response
210
- self.close()
211
- raise GeneralProxyError((1, _generalerrors[1]))
212
- if authstat[1] != "\x00":
213
- # Authentication failed
214
- self.close()
215
- raise Socks5AuthError((3, _socks5autherrors[3]))
216
- # Authentication succeeded
217
- else:
218
- # Reaching here is always bad
219
- self.close()
220
- if chosenauth[1] == "\xFF":
221
- raise Socks5AuthError((2, _socks5autherrors[2]))
222
- else:
223
- raise GeneralProxyError((1, _generalerrors[1]))
224
- # Now we can request the actual connection
225
- req = "\x05\x01\x00"
226
- # If the given destination address is an IP address, we'll
227
- # use the IPv4 address request even if remote resolving was specified.
228
- try:
229
- ipaddr = socket.inet_aton(destaddr)
230
- req = req + "\x01" + ipaddr
231
- except socket.error:
232
- # Well it's not an IP number, so it's probably a DNS name.
233
- if self.__proxy[3] == True:
234
- # Resolve remotely
235
- ipaddr = None
236
- req = req + "\x03" + chr(len(destaddr)) + destaddr
237
- else:
238
- # Resolve locally
239
- ipaddr = socket.inet_aton(socket.gethostbyname(destaddr))
240
- req = req + "\x01" + ipaddr
241
- req = req + struct.pack(">H", destport)
242
- self.sendall(req)
243
- # Get the response
244
- resp = self.__recvall(4)
245
- if resp[0] != "\x05":
246
- self.close()
247
- raise GeneralProxyError((1, _generalerrors[1]))
248
- elif resp[1] != "\x00":
249
- # Connection failed
250
- self.close()
251
- if ord(resp[1]) <= 8:
252
- raise Socks5Error(ord(resp[1]), _generalerrors[ord(resp[1])])
253
- else:
254
- raise Socks5Error(9, _generalerrors[9])
255
- # Get the bound address/port
256
- elif resp[3] == "\x01":
257
- boundaddr = self.__recvall(4)
258
- elif resp[3] == "\x03":
259
- resp = resp + self.recv(1)
260
- boundaddr = self.__recvall(resp[4])
261
- else:
262
- self.close()
263
- raise GeneralProxyError((1, _generalerrors[1]))
264
- boundport = struct.unpack(">H", self.__recvall(2))[0]
265
- self.__proxysockname = (boundaddr, boundport)
266
- if ipaddr != None:
267
- self.__proxypeername = (socket.inet_ntoa(ipaddr), destport)
268
- else:
269
- self.__proxypeername = (destaddr, destport)
270
-
271
- def getproxysockname(self):
272
- """getsockname() -> address info
273
- Returns the bound IP address and port number at the proxy.
274
- """
275
- return self.__proxysockname
276
-
277
- def getproxypeername(self):
278
- """getproxypeername() -> address info
279
- Returns the IP and port number of the proxy.
280
- """
281
- return _orgsocket.getpeername(self)
282
-
283
- def getpeername(self):
284
- """getpeername() -> address info
285
- Returns the IP address and port number of the destination
286
- machine (note: getproxypeername returns the proxy)
287
- """
288
- return self.__proxypeername
289
-
290
- def __negotiatesocks4(self, destaddr, destport):
291
- """__negotiatesocks4(self,destaddr,destport)
292
- Negotiates a connection through a SOCKS4 server.
293
- """
294
- # Check if the destination address provided is an IP address
295
- rmtrslv = False
296
- try:
297
- ipaddr = socket.inet_aton(destaddr)
298
- except socket.error:
299
- # It's a DNS name. Check where it should be resolved.
300
- if self.__proxy[3] == True:
301
- ipaddr = "\x00\x00\x00\x01"
302
- rmtrslv = True
303
- else:
304
- ipaddr = socket.inet_aton(socket.gethostbyname(destaddr))
305
- # Construct the request packet
306
- req = "\x04\x01" + struct.pack(">H", destport) + ipaddr
307
- # The username parameter is considered userid for SOCKS4
308
- if self.__proxy[4] != None:
309
- req = req + self.__proxy[4]
310
- req = req + "\x00"
311
- # DNS name if remote resolving is required
312
- # NOTE: This is actually an extension to the SOCKS4 protocol
313
- # called SOCKS4A and may not be supported in all cases.
314
- if rmtrslv == True:
315
- req = req + destaddr + "\x00"
316
- self.sendall(req)
317
- # Get the response from the server
318
- resp = self.__recvall(8)
319
- if resp[0] != "\x00":
320
- # Bad data
321
- self.close()
322
- raise GeneralProxyError((1, _generalerrors[1]))
323
- if resp[1] != "\x5A":
324
- # Server returned an error
325
- self.close()
326
- if ord(resp[1]) in (91, 92, 93):
327
- self.close()
328
- raise Socks4Error((ord(resp[1]), _socks4errors[ord(resp[1]) - 90]))
329
- else:
330
- raise Socks4Error((94, _socks4errors[4]))
331
- # Get the bound address/port
332
- self.__proxysockname = (socket.inet_ntoa(resp[4:]), struct.unpack(">H", resp[2:4])[0])
333
- if rmtrslv != None:
334
- self.__proxypeername = (socket.inet_ntoa(ipaddr), destport)
335
- else:
336
- self.__proxypeername = (destaddr, destport)
337
-
338
- def __negotiatehttp(self, destaddr, destport):
339
- """__negotiatehttp(self,destaddr,destport)
340
- Negotiates a connection through an HTTP server.
341
- """
342
- # If we need to resolve locally, we do this now
343
- if self.__proxy[3] == False:
344
- addr = socket.gethostbyname(destaddr)
345
- else:
346
- addr = destaddr
347
- self.sendall("CONNECT " + addr + ":" + str(destport) + " HTTP/1.1\r\n" + "Host: " + destaddr + "\r\n\r\n")
348
- # We read the response until we get the string "\r\n\r\n"
349
- resp = self.recv(1)
350
- while resp.find("\r\n\r\n") == -1:
351
- resp = resp + self.recv(1)
352
- # We just need the first line to check if the connection
353
- # was successful
354
- statusline = resp.splitlines()[0].split(" ", 2)
355
- if statusline[0] not in ("HTTP/1.0", "HTTP/1.1"):
356
- self.close()
357
- raise GeneralProxyError((1, _generalerrors[1]))
358
- try:
359
- statuscode = int(statusline[1])
360
- except ValueError:
361
- self.close()
362
- raise GeneralProxyError((1, _generalerrors[1]))
363
- if statuscode != 200:
364
- self.close()
365
- raise HTTPError((statuscode, statusline[2]))
366
- self.__proxysockname = ("0.0.0.0", 0)
367
- self.__proxypeername = (addr, destport)
368
-
369
- def connect(self, destpair):
370
- """connect(self,despair)
371
- Connects to the specified destination through a proxy.
372
- destpar - A tuple of the IP/DNS address and the port number.
373
- (identical to socket's connect).
374
- To select the proxy server use setproxy().
375
- """
376
- # Do a minimal input check first
377
- if (type(destpair) in (list, tuple) == False) or (len(destpair) < 2) or (type(destpair[0]) != str) or (
378
- type(destpair[1]) != int):
379
- raise GeneralProxyError((5, _generalerrors[5]))
380
- if self.__proxy[0] == PROXY_TYPE_SOCKS5:
381
- if self.__proxy[2] != None:
382
- portnum = self.__proxy[2]
383
- else:
384
- portnum = 1080
385
- _orgsocket.connect(self, (self.__proxy[1], portnum))
386
- self.__negotiatesocks5(destpair[0], destpair[1])
387
- elif self.__proxy[0] == PROXY_TYPE_SOCKS4:
388
- if self.__proxy[2] != None:
389
- portnum = self.__proxy[2]
390
- else:
391
- portnum = 1080
392
- _orgsocket.connect(self, (self.__proxy[1], portnum))
393
- self.__negotiatesocks4(destpair[0], destpair[1])
394
- elif self.__proxy[0] == PROXY_TYPE_HTTP:
395
- if self.__proxy[2] != None:
396
- portnum = self.__proxy[2]
397
- else:
398
- portnum = 8080
399
- _orgsocket.connect(self, (self.__proxy[1], portnum))
400
- self.__negotiatehttp(destpair[0], destpair[1])
401
- elif self.__proxy[0] == None:
402
- _orgsocket.connect(self, (destpair[0], destpair[1]))
403
- else:
404
- raise GeneralProxyError((4, _generalerrors[4]))
1
+ """SocksiPy - Python SOCKS module.
2
+ Version 1.00
3
+
4
+ Copyright 2006 Dan-Haim. All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without modification,
7
+ are permitted provided that the following conditions are met:
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+ 2. Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+ 3. Neither the name of Dan Haim nor the names of his contributors may be used
14
+ to endorse or promote products derived from this software without specific
15
+ prior written permission.
16
+
17
+ THIS SOFTWARE IS PROVIDED BY DAN HAIM "AS IS" AND ANY EXPRESS OR IMPLIED
18
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20
+ EVENT SHALL DAN HAIM OR HIS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA
23
+ OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25
+ OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMANGE.
26
+
27
+
28
+ This module provides a standard socket-like interface for Python
29
+ for tunneling connections through SOCKS proxies.
30
+
31
+ """
32
+
33
+ import socket
34
+ import struct
35
+
36
+ PROXY_TYPE_SOCKS4 = 1
37
+ PROXY_TYPE_SOCKS5 = 2
38
+ PROXY_TYPE_HTTP = 3
39
+
40
+ _defaultproxy = None
41
+ _orgsocket = socket.socket
42
+
43
+
44
+ class ProxyError(Exception):
45
+ def __init__(self, value):
46
+ self.value = value
47
+
48
+ def __str__(self):
49
+ return repr(self.value)
50
+
51
+
52
+ class GeneralProxyError(ProxyError):
53
+ def __init__(self, value):
54
+ self.value = value
55
+
56
+ def __str__(self):
57
+ return repr(self.value)
58
+
59
+
60
+ class Socks5AuthError(ProxyError):
61
+ def __init__(self, value):
62
+ self.value = value
63
+
64
+ def __str__(self):
65
+ return repr(self.value)
66
+
67
+
68
+ class Socks5Error(ProxyError):
69
+ def __init__(self, value):
70
+ self.value = value
71
+
72
+ def __str__(self):
73
+ return repr(self.value)
74
+
75
+
76
+ class Socks4Error(ProxyError):
77
+ def __init__(self, value):
78
+ self.value = value
79
+
80
+ def __str__(self):
81
+ return repr(self.value)
82
+
83
+
84
+ class HTTPError(ProxyError):
85
+ def __init__(self, value):
86
+ self.value = value
87
+
88
+ def __str__(self):
89
+ return repr(self.value)
90
+
91
+
92
+ _generalerrors = ("success",
93
+ "invalid data",
94
+ "not connected",
95
+ "not available",
96
+ "bad proxy type",
97
+ "bad input")
98
+
99
+ _socks5errors = ("succeeded",
100
+ "general SOCKS server failure",
101
+ "connection not allowed by ruleset",
102
+ "Network unreachable",
103
+ "Host unreachable",
104
+ "Connection refused",
105
+ "TTL expired",
106
+ "Command not supported",
107
+ "Address type not supported",
108
+ "Unknown error")
109
+
110
+ _socks5autherrors = ("succeeded",
111
+ "authentication is required",
112
+ "all offered authentication methods were rejected",
113
+ "unknown username or invalid password",
114
+ "unknown error")
115
+
116
+ _socks4errors = ("request granted",
117
+ "request rejected or failed",
118
+ "request rejected because SOCKS server cannot connect to identd on the client",
119
+ "request rejected because the client program and identd report different user-ids",
120
+ "unknown error")
121
+
122
+
123
+ def setdefaultproxy(proxytype=None, addr=None, port=None, rdns=True, username=None, password=None):
124
+ """setdefaultproxy(proxytype, addr[, port[, rdns[, username[, password]]]])
125
+ Sets a default proxy which all further socksocket objects will use,
126
+ unless explicitly changed.
127
+ """
128
+ global _defaultproxy
129
+ _defaultproxy = (proxytype, addr, port, rdns, username, password)
130
+
131
+
132
+ class socksocket(socket.socket):
133
+ """socksocket([family[, type[, proto]]]) -> socket object
134
+
135
+ Open a SOCKS enabled socket. The parameters are the same as
136
+ those of the standard socket init. In order for SOCKS to work,
137
+ you must specify family=AF_INET, type=SOCK_STREAM and proto=0.
138
+ """
139
+
140
+ def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, _sock=None):
141
+ _orgsocket.__init__(self, family, type, proto, _sock)
142
+ if _defaultproxy != None:
143
+ self.__proxy = _defaultproxy
144
+ else:
145
+ self.__proxy = (None, None, None, None, None, None)
146
+ self.__proxysockname = None
147
+ self.__proxypeername = None
148
+
149
+ def __recvall(self, bytes):
150
+ """__recvall(bytes) -> data
151
+ Receive EXACTLY the number of bytes requested from the socket.
152
+ Blocks until the required number of bytes have been received.
153
+ """
154
+ data = ""
155
+ while len(data) < bytes:
156
+ data = data + self.recv(bytes - len(data))
157
+ return data
158
+
159
+ def setproxy(self, proxytype=None, addr=None, port=None, rdns=True, username=None, password=None):
160
+ """setproxy(proxytype, addr[, port[, rdns[, username[, password]]]])
161
+ Sets the proxy to be used.
162
+ proxytype - The type of the proxy to be used. Three types
163
+ are supported: PROXY_TYPE_SOCKS4 (including socks4a),
164
+ PROXY_TYPE_SOCKS5 and PROXY_TYPE_HTTP
165
+ addr - The address of the server (IP or DNS).
166
+ port - The port of the server. Defaults to 1080 for SOCKS
167
+ servers and 8080 for HTTP proxy servers.
168
+ rdns - Should DNS queries be preformed on the remote side
169
+ (rather than the local side). The default is True.
170
+ Note: This has no effect with SOCKS4 servers.
171
+ username - Username to authenticate with to the server.
172
+ The default is no authentication.
173
+ password - Password to authenticate with to the server.
174
+ Only relevant when username is also provided.
175
+ """
176
+ self.__proxy = (proxytype, addr, port, rdns, username, password)
177
+
178
+ def __negotiatesocks5(self, destaddr, destport):
179
+ """__negotiatesocks5(self,destaddr,destport)
180
+ Negotiates a connection through a SOCKS5 server.
181
+ """
182
+ # First we'll send the authentication packages we support.
183
+ if (self.__proxy[4] != None) and (self.__proxy[5] != None):
184
+ # The username/password details were supplied to the
185
+ # setproxy method so we support the USERNAME/PASSWORD
186
+ # authentication (in addition to the standard none).
187
+ self.sendall("\x05\x02\x00\x02")
188
+ else:
189
+ # No username/password were entered, therefore we
190
+ # only support connections with no authentication.
191
+ self.sendall("\x05\x01\x00")
192
+ # We'll receive the server's response to determine which
193
+ # method was selected
194
+ chosenauth = self.__recvall(2)
195
+ if chosenauth[0] != "\x05":
196
+ self.close()
197
+ raise GeneralProxyError((1, _generalerrors[1]))
198
+ # Check the chosen authentication method
199
+ if chosenauth[1] == "\x00":
200
+ # No authentication is required
201
+ pass
202
+ elif chosenauth[1] == "\x02":
203
+ # Okay, we need to perform a basic username/password
204
+ # authentication.
205
+ self.sendall(
206
+ "\x01" + chr(len(self.__proxy[4])) + self.__proxy[4] + chr(len(self.proxy[5])) + self.__proxy[5])
207
+ authstat = self.__recvall(2)
208
+ if authstat[0] != "\x01":
209
+ # Bad response
210
+ self.close()
211
+ raise GeneralProxyError((1, _generalerrors[1]))
212
+ if authstat[1] != "\x00":
213
+ # Authentication failed
214
+ self.close()
215
+ raise Socks5AuthError((3, _socks5autherrors[3]))
216
+ # Authentication succeeded
217
+ else:
218
+ # Reaching here is always bad
219
+ self.close()
220
+ if chosenauth[1] == "\xFF":
221
+ raise Socks5AuthError((2, _socks5autherrors[2]))
222
+ else:
223
+ raise GeneralProxyError((1, _generalerrors[1]))
224
+ # Now we can request the actual connection
225
+ req = "\x05\x01\x00"
226
+ # If the given destination address is an IP address, we'll
227
+ # use the IPv4 address request even if remote resolving was specified.
228
+ try:
229
+ ipaddr = socket.inet_aton(destaddr)
230
+ req = req + "\x01" + ipaddr
231
+ except socket.error:
232
+ # Well it's not an IP number, so it's probably a DNS name.
233
+ if self.__proxy[3] == True:
234
+ # Resolve remotely
235
+ ipaddr = None
236
+ req = req + "\x03" + chr(len(destaddr)) + destaddr
237
+ else:
238
+ # Resolve locally
239
+ ipaddr = socket.inet_aton(socket.gethostbyname(destaddr))
240
+ req = req + "\x01" + ipaddr
241
+ req = req + struct.pack(">H", destport)
242
+ self.sendall(req)
243
+ # Get the response
244
+ resp = self.__recvall(4)
245
+ if resp[0] != "\x05":
246
+ self.close()
247
+ raise GeneralProxyError((1, _generalerrors[1]))
248
+ elif resp[1] != "\x00":
249
+ # Connection failed
250
+ self.close()
251
+ if ord(resp[1]) <= 8:
252
+ raise Socks5Error(ord(resp[1]), _generalerrors[ord(resp[1])])
253
+ else:
254
+ raise Socks5Error(9, _generalerrors[9])
255
+ # Get the bound address/port
256
+ elif resp[3] == "\x01":
257
+ boundaddr = self.__recvall(4)
258
+ elif resp[3] == "\x03":
259
+ resp = resp + self.recv(1)
260
+ boundaddr = self.__recvall(resp[4])
261
+ else:
262
+ self.close()
263
+ raise GeneralProxyError((1, _generalerrors[1]))
264
+ boundport = struct.unpack(">H", self.__recvall(2))[0]
265
+ self.__proxysockname = (boundaddr, boundport)
266
+ if ipaddr != None:
267
+ self.__proxypeername = (socket.inet_ntoa(ipaddr), destport)
268
+ else:
269
+ self.__proxypeername = (destaddr, destport)
270
+
271
+ def getproxysockname(self):
272
+ """getsockname() -> address info
273
+ Returns the bound IP address and port number at the proxy.
274
+ """
275
+ return self.__proxysockname
276
+
277
+ def getproxypeername(self):
278
+ """getproxypeername() -> address info
279
+ Returns the IP and port number of the proxy.
280
+ """
281
+ return _orgsocket.getpeername(self)
282
+
283
+ def getpeername(self):
284
+ """getpeername() -> address info
285
+ Returns the IP address and port number of the destination
286
+ machine (note: getproxypeername returns the proxy)
287
+ """
288
+ return self.__proxypeername
289
+
290
+ def __negotiatesocks4(self, destaddr, destport):
291
+ """__negotiatesocks4(self,destaddr,destport)
292
+ Negotiates a connection through a SOCKS4 server.
293
+ """
294
+ # Check if the destination address provided is an IP address
295
+ rmtrslv = False
296
+ try:
297
+ ipaddr = socket.inet_aton(destaddr)
298
+ except socket.error:
299
+ # It's a DNS name. Check where it should be resolved.
300
+ if self.__proxy[3] == True:
301
+ ipaddr = "\x00\x00\x00\x01"
302
+ rmtrslv = True
303
+ else:
304
+ ipaddr = socket.inet_aton(socket.gethostbyname(destaddr))
305
+ # Construct the request packet
306
+ req = "\x04\x01" + struct.pack(">H", destport) + ipaddr
307
+ # The username parameter is considered userid for SOCKS4
308
+ if self.__proxy[4] != None:
309
+ req = req + self.__proxy[4]
310
+ req = req + "\x00"
311
+ # DNS name if remote resolving is required
312
+ # NOTE: This is actually an extension to the SOCKS4 protocol
313
+ # called SOCKS4A and may not be supported in all cases.
314
+ if rmtrslv == True:
315
+ req = req + destaddr + "\x00"
316
+ self.sendall(req)
317
+ # Get the response from the server
318
+ resp = self.__recvall(8)
319
+ if resp[0] != "\x00":
320
+ # Bad data
321
+ self.close()
322
+ raise GeneralProxyError((1, _generalerrors[1]))
323
+ if resp[1] != "\x5A":
324
+ # Server returned an error
325
+ self.close()
326
+ if ord(resp[1]) in (91, 92, 93):
327
+ self.close()
328
+ raise Socks4Error((ord(resp[1]), _socks4errors[ord(resp[1]) - 90]))
329
+ else:
330
+ raise Socks4Error((94, _socks4errors[4]))
331
+ # Get the bound address/port
332
+ self.__proxysockname = (socket.inet_ntoa(resp[4:]), struct.unpack(">H", resp[2:4])[0])
333
+ if rmtrslv != None:
334
+ self.__proxypeername = (socket.inet_ntoa(ipaddr), destport)
335
+ else:
336
+ self.__proxypeername = (destaddr, destport)
337
+
338
+ def __negotiatehttp(self, destaddr, destport):
339
+ """__negotiatehttp(self,destaddr,destport)
340
+ Negotiates a connection through an HTTP server.
341
+ """
342
+ # If we need to resolve locally, we do this now
343
+ if self.__proxy[3] == False:
344
+ addr = socket.gethostbyname(destaddr)
345
+ else:
346
+ addr = destaddr
347
+ self.sendall("CONNECT " + addr + ":" + str(destport) + " HTTP/1.1\r\n" + "Host: " + destaddr + "\r\n\r\n")
348
+ # We read the response until we get the string "\r\n\r\n"
349
+ resp = self.recv(1)
350
+ while resp.find("\r\n\r\n") == -1:
351
+ resp = resp + self.recv(1)
352
+ # We just need the first line to check if the connection
353
+ # was successful
354
+ statusline = resp.splitlines()[0].split(" ", 2)
355
+ if statusline[0] not in ("HTTP/1.0", "HTTP/1.1"):
356
+ self.close()
357
+ raise GeneralProxyError((1, _generalerrors[1]))
358
+ try:
359
+ statuscode = int(statusline[1])
360
+ except ValueError:
361
+ self.close()
362
+ raise GeneralProxyError((1, _generalerrors[1]))
363
+ if statuscode != 200:
364
+ self.close()
365
+ raise HTTPError((statuscode, statusline[2]))
366
+ self.__proxysockname = ("0.0.0.0", 0)
367
+ self.__proxypeername = (addr, destport)
368
+
369
+ def connect(self, destpair):
370
+ """connect(self,despair)
371
+ Connects to the specified destination through a proxy.
372
+ destpar - A tuple of the IP/DNS address and the port number.
373
+ (identical to socket's connect).
374
+ To select the proxy server use setproxy().
375
+ """
376
+ # Do a minimal input check first
377
+ if (type(destpair) in (list, tuple) == False) or (len(destpair) < 2) or (type(destpair[0]) != str) or (
378
+ type(destpair[1]) != int):
379
+ raise GeneralProxyError((5, _generalerrors[5]))
380
+ if self.__proxy[0] == PROXY_TYPE_SOCKS5:
381
+ if self.__proxy[2] != None:
382
+ portnum = self.__proxy[2]
383
+ else:
384
+ portnum = 1080
385
+ _orgsocket.connect(self, (self.__proxy[1], portnum))
386
+ self.__negotiatesocks5(destpair[0], destpair[1])
387
+ elif self.__proxy[0] == PROXY_TYPE_SOCKS4:
388
+ if self.__proxy[2] != None:
389
+ portnum = self.__proxy[2]
390
+ else:
391
+ portnum = 1080
392
+ _orgsocket.connect(self, (self.__proxy[1], portnum))
393
+ self.__negotiatesocks4(destpair[0], destpair[1])
394
+ elif self.__proxy[0] == PROXY_TYPE_HTTP:
395
+ if self.__proxy[2] != None:
396
+ portnum = self.__proxy[2]
397
+ else:
398
+ portnum = 8080
399
+ _orgsocket.connect(self, (self.__proxy[1], portnum))
400
+ self.__negotiatehttp(destpair[0], destpair[1])
401
+ elif self.__proxy[0] == None:
402
+ _orgsocket.connect(self, (destpair[0], destpair[1]))
403
+ else:
404
+ raise GeneralProxyError((4, _generalerrors[4]))