@@ -180,7 +180,7 @@ def cipmux(self) -> int:
180
180
return int (reply [8 :])
181
181
raise RuntimeError ("Bad response to CIPMUX?" )
182
182
183
- def socket_connect (
183
+ def socket_connect ( # pylint: disable=too-many-branches
184
184
self ,
185
185
conntype : str ,
186
186
remote : str ,
@@ -191,7 +191,31 @@ def socket_connect(
191
191
) -> bool :
192
192
"""Open a socket. conntype can be TYPE_TCP, TYPE_UDP, or TYPE_SSL. Remote
193
193
can be an IP address or DNS (we'll do the lookup for you. Remote port
194
- is integer port on other side. We can't set the local port"""
194
+ is integer port on other side. We can't set the local port.
195
+
196
+ Note that this method is usually called by the requests-package, which
197
+ does not know anything about conntype. So it is mandatory to set
198
+ the conntype manually before calling this method if the conntype-parameter
199
+ is not provided.
200
+
201
+ If requests are done using ESPAT_WiFiManager, the conntype is set there
202
+ depending on the protocol (http/https)."""
203
+
204
+ # if caller does not provide conntype, use default conntype from
205
+ # object if set, otherwise fall back to old buggy logic
206
+ if not conntype and self ._conntype :
207
+ conntype = self ._conntype
208
+ elif not conntype :
209
+ # old buggy code from espatcontrol_socket
210
+ # added here for compatibility with old code
211
+ if remote_port == 80 :
212
+ conntype = self .TYPE_TCP
213
+ elif remote_port == 443 :
214
+ conntype = self .TYPE_SSL
215
+ # to cater for MQTT over TCP
216
+ elif remote_port == 1883 :
217
+ conntype = self .TYPE_TCP
218
+
195
219
# lets just do one connection at a time for now
196
220
if conntype == self .TYPE_UDP :
197
221
# always disconnect for TYPE_UDP
@@ -219,7 +243,7 @@ def socket_connect(
219
243
replies = self .at_response (cmd , timeout = 10 , retries = retries ).split (b"\r \n " )
220
244
for reply in replies :
221
245
if reply == b"CONNECT" and (
222
- conntype == self .TYPE_TCP
246
+ ( conntype == self .TYPE_TCP or conntype == self . TYPE_SSL )
223
247
and self .status == self .STATUS_SOCKETOPEN
224
248
or conntype == self .TYPE_UDP
225
249
):
@@ -415,6 +439,16 @@ def mode(self, mode: int) -> None:
415
439
raise RuntimeError ("Invalid Mode" )
416
440
self .at_response ("AT+CWMODE=%d" % mode , timeout = 3 )
417
441
442
+ @property
443
+ def conntype (self ) -> Union [str , None ]:
444
+ """The configured connection-type"""
445
+ return self ._conntype
446
+
447
+ @conntype .setter
448
+ def conntype (self , conntype : str ) -> None :
449
+ """set connection-type for subsequent socket_connect()"""
450
+ self ._conntype = conntype
451
+
418
452
@property
419
453
def local_ip (self ) -> Union [str , None ]:
420
454
"""Our local IP address as a dotted-quad string"""
0 commit comments