client.py 151 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950
  1. # Copyright (c) 2012-2019 Roger Light and others
  2. #
  3. # All rights reserved. This program and the accompanying materials
  4. # are made available under the terms of the Eclipse Public License v1.0
  5. # and Eclipse Distribution License v1.0 which accompany this distribution.
  6. #
  7. # The Eclipse Public License is available at
  8. # http://www.eclipse.org/legal/epl-v10.html
  9. # and the Eclipse Distribution License is available at
  10. # http://www.eclipse.org/org/documents/edl-v10.php.
  11. #
  12. # Contributors:
  13. # Roger Light - initial API and implementation
  14. # Ian Craggs - MQTT V5 support
  15. from .subscribeoptions import SubscribeOptions
  16. from .reasoncodes import ReasonCodes
  17. from .properties import Properties
  18. from .matcher import MQTTMatcher
  19. import logging
  20. import hashlib
  21. import string
  22. import base64
  23. import uuid
  24. import time
  25. import threading
  26. import sys
  27. import struct
  28. """
  29. This is an MQTT client module. MQTT is a lightweight pub/sub messaging
  30. protocol that is easy to implement and suitable for low powered devices.
  31. """
  32. import collections
  33. import errno
  34. import os
  35. import platform
  36. import socket
  37. ssl = None
  38. try:
  39. import ssl
  40. except ImportError:
  41. pass
  42. socks = None
  43. try:
  44. import socks
  45. except ImportError:
  46. pass
  47. try:
  48. # Python 3
  49. from urllib import request as urllib_dot_request
  50. from urllib import parse as urllib_dot_parse
  51. except ImportError:
  52. # Python 2
  53. import urllib as urllib_dot_request
  54. import urlparse as urllib_dot_parse
  55. try:
  56. # Use monotonic clock if available
  57. time_func = time.monotonic
  58. except AttributeError:
  59. time_func = time.time
  60. try:
  61. import dns.resolver
  62. except ImportError:
  63. HAVE_DNS = False
  64. else:
  65. HAVE_DNS = True
  66. if platform.system() == 'Windows':
  67. EAGAIN = errno.WSAEWOULDBLOCK
  68. else:
  69. EAGAIN = errno.EAGAIN
  70. MQTTv31 = 3
  71. MQTTv311 = 4
  72. MQTTv5 = 5
  73. if sys.version_info[0] >= 3:
  74. # define some alias for python2 compatibility
  75. unicode = str
  76. basestring = str
  77. # Message types
  78. CONNECT = 0x10
  79. CONNACK = 0x20
  80. PUBLISH = 0x30
  81. PUBACK = 0x40
  82. PUBREC = 0x50
  83. PUBREL = 0x60
  84. PUBCOMP = 0x70
  85. SUBSCRIBE = 0x80
  86. SUBACK = 0x90
  87. UNSUBSCRIBE = 0xA0
  88. UNSUBACK = 0xB0
  89. PINGREQ = 0xC0
  90. PINGRESP = 0xD0
  91. DISCONNECT = 0xE0
  92. AUTH = 0xF0
  93. # Log levels
  94. MQTT_LOG_INFO = 0x01
  95. MQTT_LOG_NOTICE = 0x02
  96. MQTT_LOG_WARNING = 0x04
  97. MQTT_LOG_ERR = 0x08
  98. MQTT_LOG_DEBUG = 0x10
  99. LOGGING_LEVEL = {
  100. MQTT_LOG_DEBUG: logging.DEBUG,
  101. MQTT_LOG_INFO: logging.INFO,
  102. MQTT_LOG_NOTICE: logging.INFO, # This has no direct equivalent level
  103. MQTT_LOG_WARNING: logging.WARNING,
  104. MQTT_LOG_ERR: logging.ERROR,
  105. }
  106. # CONNACK codes
  107. CONNACK_ACCEPTED = 0
  108. CONNACK_REFUSED_PROTOCOL_VERSION = 1
  109. CONNACK_REFUSED_IDENTIFIER_REJECTED = 2
  110. CONNACK_REFUSED_SERVER_UNAVAILABLE = 3
  111. CONNACK_REFUSED_BAD_USERNAME_PASSWORD = 4
  112. CONNACK_REFUSED_NOT_AUTHORIZED = 5
  113. # Connection state
  114. mqtt_cs_new = 0
  115. mqtt_cs_connected = 1
  116. mqtt_cs_disconnecting = 2
  117. mqtt_cs_connect_async = 3
  118. # Message state
  119. mqtt_ms_invalid = 0
  120. mqtt_ms_publish = 1
  121. mqtt_ms_wait_for_puback = 2
  122. mqtt_ms_wait_for_pubrec = 3
  123. mqtt_ms_resend_pubrel = 4
  124. mqtt_ms_wait_for_pubrel = 5
  125. mqtt_ms_resend_pubcomp = 6
  126. mqtt_ms_wait_for_pubcomp = 7
  127. mqtt_ms_send_pubrec = 8
  128. mqtt_ms_queued = 9
  129. # Error values
  130. MQTT_ERR_AGAIN = -1
  131. MQTT_ERR_SUCCESS = 0
  132. MQTT_ERR_NOMEM = 1
  133. MQTT_ERR_PROTOCOL = 2
  134. MQTT_ERR_INVAL = 3
  135. MQTT_ERR_NO_CONN = 4
  136. MQTT_ERR_CONN_REFUSED = 5
  137. MQTT_ERR_NOT_FOUND = 6
  138. MQTT_ERR_CONN_LOST = 7
  139. MQTT_ERR_TLS = 8
  140. MQTT_ERR_PAYLOAD_SIZE = 9
  141. MQTT_ERR_NOT_SUPPORTED = 10
  142. MQTT_ERR_AUTH = 11
  143. MQTT_ERR_ACL_DENIED = 12
  144. MQTT_ERR_UNKNOWN = 13
  145. MQTT_ERR_ERRNO = 14
  146. MQTT_ERR_QUEUE_SIZE = 15
  147. MQTT_CLIENT = 0
  148. MQTT_BRIDGE = 1
  149. # For MQTT V5, use the clean start flag only on the first successful connect
  150. MQTT_CLEAN_START_FIRST_ONLY = 3
  151. sockpair_data = b"0"
  152. class WebsocketConnectionError(ValueError):
  153. pass
  154. class WouldBlockError(Exception):
  155. pass
  156. def error_string(mqtt_errno):
  157. """Return the error string associated with an mqtt error number."""
  158. if mqtt_errno == MQTT_ERR_SUCCESS:
  159. return "No error."
  160. elif mqtt_errno == MQTT_ERR_NOMEM:
  161. return "Out of memory."
  162. elif mqtt_errno == MQTT_ERR_PROTOCOL:
  163. return "A network protocol error occurred when communicating with the broker."
  164. elif mqtt_errno == MQTT_ERR_INVAL:
  165. return "Invalid function arguments provided."
  166. elif mqtt_errno == MQTT_ERR_NO_CONN:
  167. return "The client is not currently connected."
  168. elif mqtt_errno == MQTT_ERR_CONN_REFUSED:
  169. return "The connection was refused."
  170. elif mqtt_errno == MQTT_ERR_NOT_FOUND:
  171. return "Message not found (internal error)."
  172. elif mqtt_errno == MQTT_ERR_CONN_LOST:
  173. return "The connection was lost."
  174. elif mqtt_errno == MQTT_ERR_TLS:
  175. return "A TLS error occurred."
  176. elif mqtt_errno == MQTT_ERR_PAYLOAD_SIZE:
  177. return "Payload too large."
  178. elif mqtt_errno == MQTT_ERR_NOT_SUPPORTED:
  179. return "This feature is not supported."
  180. elif mqtt_errno == MQTT_ERR_AUTH:
  181. return "Authorisation failed."
  182. elif mqtt_errno == MQTT_ERR_ACL_DENIED:
  183. return "Access denied by ACL."
  184. elif mqtt_errno == MQTT_ERR_UNKNOWN:
  185. return "Unknown error."
  186. elif mqtt_errno == MQTT_ERR_ERRNO:
  187. return "Error defined by errno."
  188. elif mqtt_errno == MQTT_ERR_QUEUE_SIZE:
  189. return "Message queue full."
  190. else:
  191. return "Unknown error."
  192. def connack_string(connack_code):
  193. """Return the string associated with a CONNACK result."""
  194. if connack_code == CONNACK_ACCEPTED:
  195. return "Connection Accepted."
  196. elif connack_code == CONNACK_REFUSED_PROTOCOL_VERSION:
  197. return "Connection Refused: unacceptable protocol version."
  198. elif connack_code == CONNACK_REFUSED_IDENTIFIER_REJECTED:
  199. return "Connection Refused: identifier rejected."
  200. elif connack_code == CONNACK_REFUSED_SERVER_UNAVAILABLE:
  201. return "Connection Refused: broker unavailable."
  202. elif connack_code == CONNACK_REFUSED_BAD_USERNAME_PASSWORD:
  203. return "Connection Refused: bad user name or password."
  204. elif connack_code == CONNACK_REFUSED_NOT_AUTHORIZED:
  205. return "Connection Refused: not authorised."
  206. else:
  207. return "Connection Refused: unknown reason."
  208. def base62(num, base = string.digits + string.ascii_letters, padding = 1):
  209. """Convert a number to base-62 representation."""
  210. assert num >= 0
  211. digits = []
  212. while num:
  213. num, rest = divmod(num, 62)
  214. digits.append(base[rest])
  215. digits.extend(base[0] for _ in range(len(digits), padding))
  216. return ''.join(reversed(digits))
  217. def topic_matches_sub(sub, topic):
  218. """Check whether a topic matches a subscription.
  219. For example:
  220. foo/bar would match the subscription foo/# or +/bar
  221. non/matching would not match the subscription non/+/+
  222. """
  223. matcher = MQTTMatcher()
  224. matcher[sub] = True
  225. try:
  226. next(matcher.iter_match(topic))
  227. return True
  228. except StopIteration:
  229. return False
  230. def _socketpair_compat():
  231. """TCP/IP socketpair including Windows support"""
  232. listensock = socket.socket(
  233. socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
  234. listensock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  235. listensock.bind(("127.0.0.1", 0))
  236. listensock.listen(1)
  237. iface, port = listensock.getsockname()
  238. sock1 = socket.socket(
  239. socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_IP)
  240. sock1.setblocking(0)
  241. try:
  242. sock1.connect(("127.0.0.1", port))
  243. except socket.error as err:
  244. if err.errno != errno.EINPROGRESS and err.errno != errno.EWOULDBLOCK and err.errno != EAGAIN:
  245. raise
  246. sock2, address = listensock.accept()
  247. sock2.setblocking(0)
  248. listensock.close()
  249. return (sock1, sock2)
  250. class MQTTMessageInfo(object):
  251. """This is a class returned from Client.publish() and can be used to find
  252. out the mid of the message that was published, and to determine whether the
  253. message has been published, and/or wait until it is published.
  254. """
  255. __slots__ = 'mid', '_published', '_condition', 'rc', '_iterpos'
  256. def __init__(self, mid):
  257. self.mid = mid
  258. self._published = False
  259. self._condition = threading.Condition()
  260. self.rc = 0
  261. self._iterpos = 0
  262. def __str__(self):
  263. return str((self.rc, self.mid))
  264. def __iter__(self):
  265. self._iterpos = 0
  266. return self
  267. def __next__(self):
  268. return self.next()
  269. def next(self):
  270. if self._iterpos == 0:
  271. self._iterpos = 1
  272. return self.rc
  273. elif self._iterpos == 1:
  274. self._iterpos = 2
  275. return self.mid
  276. else:
  277. raise StopIteration
  278. def __getitem__(self, index):
  279. if index == 0:
  280. return self.rc
  281. elif index == 1:
  282. return self.mid
  283. else:
  284. raise IndexError("index out of range")
  285. def _set_as_published(self):
  286. with self._condition:
  287. self._published = True
  288. self._condition.notify()
  289. def wait_for_publish(self):
  290. """Block until the message associated with this object is published."""
  291. if self.rc == MQTT_ERR_QUEUE_SIZE:
  292. raise ValueError('Message is not queued due to ERR_QUEUE_SIZE')
  293. with self._condition:
  294. while not self._published:
  295. self._condition.wait()
  296. def is_published(self):
  297. """Returns True if the message associated with this object has been
  298. published, else returns False."""
  299. if self.rc == MQTT_ERR_QUEUE_SIZE:
  300. raise ValueError('Message is not queued due to ERR_QUEUE_SIZE')
  301. with self._condition:
  302. return self._published
  303. class MQTTMessage(object):
  304. """ This is a class that describes an incoming or outgoing message. It is
  305. passed to the on_message callback as the message parameter.
  306. Members:
  307. topic : String/bytes. topic that the message was published on.
  308. payload : String/bytes the message payload.
  309. qos : Integer. The message Quality of Service 0, 1 or 2.
  310. retain : Boolean. If true, the message is a retained message and not fresh.
  311. mid : Integer. The message id.
  312. properties: Properties class. In MQTT v5.0, the properties associated with the message.
  313. On Python 3, topic must be bytes.
  314. """
  315. __slots__ = 'timestamp', 'state', 'dup', 'mid', '_topic', 'payload', 'qos', 'retain', 'info', 'properties'
  316. def __init__(self, mid = 0, topic = b""):
  317. self.timestamp = 0
  318. self.state = mqtt_ms_invalid
  319. self.dup = False
  320. self.mid = mid
  321. self._topic = topic
  322. self.payload = b""
  323. self.qos = 0
  324. self.retain = False
  325. self.info = MQTTMessageInfo(mid)
  326. def __eq__(self, other):
  327. """Override the default Equals behavior"""
  328. if isinstance(other, self.__class__):
  329. return self.mid == other.mid
  330. return False
  331. def __ne__(self, other):
  332. """Define a non-equality test"""
  333. return not self.__eq__(other)
  334. @property
  335. def topic(self):
  336. return self._topic.decode('utf-8')
  337. @topic.setter
  338. def topic(self, value):
  339. self._topic = value
  340. class Client(object):
  341. """MQTT version 3.1/3.1.1/5.0 client class.
  342. This is the main class for use communicating with an MQTT broker.
  343. General usage flow:
  344. * Use connect()/connect_async() to connect to a broker
  345. * Call loop() frequently to maintain network traffic flow with the broker
  346. * Or use loop_start() to set a thread running to call loop() for you.
  347. * Or use loop_forever() to handle calling loop() for you in a blocking
  348. * function.
  349. * Use subscribe() to subscribe to a topic and receive messages
  350. * Use publish() to send messages
  351. * Use disconnect() to disconnect from the broker
  352. Data returned from the broker is made available with the use of callback
  353. functions as described below.
  354. Callbacks
  355. =========
  356. A number of callback functions are available to receive data back from the
  357. broker. To use a callback, define a function and then assign it to the
  358. client:
  359. def on_connect(client, userdata, flags, rc, properties=None):
  360. print("Connection returned " + str(rc))
  361. client.on_connect = on_connect
  362. All of the callbacks as described below have a "client" and an "userdata"
  363. argument. "client" is the Client instance that is calling the callback.
  364. "userdata" is user data of any type and can be set when creating a new client
  365. instance or with user_data_set(userdata).
  366. If you wish to suppress exceptions within a callback, you should set
  367. `client.suppress_exceptions = True`
  368. The callbacks:
  369. on_connect(client, userdata, flags, rc, properties=None): called when the broker responds to our connection
  370. request.
  371. flags is a dict that contains response flags from the broker:
  372. flags['session present'] - this flag is useful for clients that are
  373. using clean session set to 0 only. If a client with clean
  374. session=0, that reconnects to a broker that it has previously
  375. connected to, this flag indicates whether the broker still has the
  376. session information for the client. If 1, the session still exists.
  377. The value of rc determines success or not:
  378. 0: Connection successful
  379. 1: Connection refused - incorrect protocol version
  380. 2: Connection refused - invalid client identifier
  381. 3: Connection refused - server unavailable
  382. 4: Connection refused - bad username or password
  383. 5: Connection refused - not authorised
  384. 6-255: Currently unused.
  385. on_disconnect(client, userdata, rc): called when the client disconnects from the broker.
  386. The rc parameter indicates the disconnection state. If MQTT_ERR_SUCCESS
  387. (0), the callback was called in response to a disconnect() call. If any
  388. other value the disconnection was unexpected, such as might be caused by
  389. a network error.
  390. on_disconnect(client, userdata, rc, properties): called when the MQTT V5 client disconnects from the broker.
  391. When using MQTT V5, the broker can send a disconnect message to the client. The
  392. message can contain a reason code and MQTT V5 properties. The properties parameter could be
  393. None if they do not exist in the disconnect message.
  394. on_message(client, userdata, message): called when a message has been received on a
  395. topic that the client subscribes to. The message variable is a
  396. MQTTMessage that describes all of the message parameters.
  397. on_publish(client, userdata, mid): called when a message that was to be sent using the
  398. publish() call has completed transmission to the broker. For messages
  399. with QoS levels 1 and 2, this means that the appropriate handshakes have
  400. completed. For QoS 0, this simply means that the message has left the
  401. client. The mid variable matches the mid variable returned from the
  402. corresponding publish() call, to allow outgoing messages to be tracked.
  403. This callback is important because even if the publish() call returns
  404. success, it does not always mean that the message has been sent.
  405. on_subscribe(client, userdata, mid, granted_qos, properties=None): called when the broker responds to a
  406. subscribe request. The mid variable matches the mid variable returned
  407. from the corresponding subscribe() call. The granted_qos variable is a
  408. list of integers that give the QoS level the broker has granted for each
  409. of the different subscription requests.
  410. on_unsubscribe(client, userdata, mid): called when the broker responds to an unsubscribe
  411. request. The mid variable matches the mid variable returned from the
  412. corresponding unsubscribe() call.
  413. on_log(client, userdata, level, buf): called when the client has log information. Define
  414. to allow debugging. The level variable gives the severity of the message
  415. and will be one of MQTT_LOG_INFO, MQTT_LOG_NOTICE, MQTT_LOG_WARNING,
  416. MQTT_LOG_ERR, and MQTT_LOG_DEBUG. The message itself is in buf.
  417. on_socket_open(client, userdata, sock): Called when the socket has been opened. Use this
  418. to register the socket with an external event loop for reading.
  419. on_socket_close(client, userdata, sock): Called when the socket is about to be closed.
  420. Use this to unregister a socket from an external event loop for reading.
  421. on_socket_register_write(client, userdata, sock): Called when a write operation to the
  422. socket failed because it would have blocked, e.g. output buffer full. Use this to
  423. register the socket with an external event loop for writing.
  424. on_socket_unregister_write(client, userdata, sock): Called when a write operation to the
  425. socket succeeded after it had previously failed. Use this to unregister the socket
  426. from an external event loop for writing.
  427. """
  428. def __init__(self, client_id = "", clean_session = None, userdata = None,
  429. protocol = MQTTv311, transport = "tcp"):
  430. """client_id is the unique client id string used when connecting to the
  431. broker. If client_id is zero length or None, then the behaviour is
  432. defined by which protocol version is in use. If using MQTT v3.1.1, then
  433. a zero length client id will be sent to the broker and the broker will
  434. generate a random for the client. If using MQTT v3.1 then an id will be
  435. randomly generated. In both cases, clean_session must be True. If this
  436. is not the case a ValueError will be raised.
  437. clean_session is a boolean that determines the client type. If True,
  438. the broker will remove all information about this client when it
  439. disconnects. If False, the client is a persistent client and
  440. subscription information and queued messages will be retained when the
  441. client disconnects.
  442. Note that a client will never discard its own outgoing messages on
  443. disconnect. Calling connect() or reconnect() will cause the messages to
  444. be resent. Use reinitialise() to reset a client to its original state.
  445. The clean_session argument only applies to MQTT versions v3.1.1 and v3.1.
  446. It is not accepted if the MQTT version is v5.0 - use the clean_start
  447. argument on connect() instead.
  448. userdata is user defined data of any type that is passed as the "userdata"
  449. parameter to callbacks. It may be updated at a later point with the
  450. user_data_set() function.
  451. The protocol argument allows explicit setting of the MQTT version to
  452. use for this client. Can be paho.mqtt.client.MQTTv311 (v3.1.1),
  453. paho.mqtt.client.MQTTv31 (v3.1) or paho.mqtt.client.MQTTv5 (v5.0),
  454. with the default being v3.1.1.
  455. Set transport to "websockets" to use WebSockets as the transport
  456. mechanism. Set to "tcp" to use raw TCP, which is the default.
  457. """
  458. if protocol == MQTTv5:
  459. if clean_session != None:
  460. raise ValueError('Clean session is not used for MQTT 5.0')
  461. else:
  462. if clean_session == None:
  463. clean_session = True
  464. if not clean_session and (client_id == "" or client_id is None):
  465. raise ValueError(
  466. 'A client id must be provided if clean session is False.')
  467. self._clean_session = clean_session
  468. if transport.lower() not in ('websockets', 'tcp'):
  469. raise ValueError(
  470. 'transport must be "websockets" or "tcp", not %s' % transport)
  471. self._transport = transport.lower()
  472. self._protocol = protocol
  473. self._userdata = userdata
  474. self._sock = None
  475. self._sockpairR, self._sockpairW = (None, None,)
  476. self._sockpairR, self._sockpairW = _socketpair_compat()
  477. self._keepalive = 60
  478. self._message_retry = 20
  479. self._last_retry_check = 0
  480. self._client_mode = MQTT_CLIENT
  481. # [MQTT-3.1.3-4] Client Id must be UTF-8 encoded string.
  482. if client_id == "" or client_id is None:
  483. if protocol == MQTTv31:
  484. self._client_id = base62(uuid.uuid4().int, padding = 22)
  485. else:
  486. self._client_id = b""
  487. else:
  488. self._client_id = client_id
  489. if isinstance(self._client_id, unicode):
  490. self._client_id = self._client_id.encode('utf-8')
  491. self._username = None
  492. self._password = None
  493. self._in_packet = {
  494. "command": 0,
  495. "have_remaining": 0,
  496. "remaining_count": [],
  497. "remaining_mult": 1,
  498. "remaining_length": 0,
  499. "packet": b"",
  500. "to_process": 0,
  501. "pos": 0}
  502. self._out_packet = collections.deque()
  503. self._current_out_packet = None
  504. self._last_msg_in = time_func()
  505. self._last_msg_out = time_func()
  506. self._reconnect_min_delay = 1
  507. self._reconnect_max_delay = 120
  508. self._reconnect_delay = None
  509. self._ping_t = 0
  510. self._last_mid = 0
  511. self._state = mqtt_cs_new
  512. self._out_messages = collections.OrderedDict()
  513. self._in_messages = collections.OrderedDict()
  514. self._max_inflight_messages = 20
  515. self._inflight_messages = 0
  516. self._max_queued_messages = 0
  517. self._connect_properties = None
  518. self._will_properties = None
  519. self._will = False
  520. self._will_topic = b""
  521. self._will_payload = b""
  522. self._will_qos = 0
  523. self._will_retain = False
  524. self._on_message_filtered = MQTTMatcher()
  525. self._host = ""
  526. self._port = 1883
  527. self._bind_address = ""
  528. self._bind_port = 0
  529. self._proxy = {}
  530. self._in_callback_mutex = threading.Lock()
  531. self._callback_mutex = threading.RLock()
  532. self._out_packet_mutex = threading.Lock()
  533. self._current_out_packet_mutex = threading.RLock()
  534. self._msgtime_mutex = threading.Lock()
  535. self._out_message_mutex = threading.RLock()
  536. self._in_message_mutex = threading.Lock()
  537. self._reconnect_delay_mutex = threading.Lock()
  538. self._mid_generate_mutex = threading.Lock()
  539. self._thread = None
  540. self._thread_terminate = False
  541. self._ssl = False
  542. self._ssl_context = None
  543. # Only used when SSL context does not have check_hostname attribute
  544. self._tls_insecure = False
  545. self._logger = None
  546. self._registered_write = False
  547. # No default callbacks
  548. self._on_log = None
  549. self._on_connect = None
  550. self._on_subscribe = None
  551. self._on_message = None
  552. self._on_publish = None
  553. self._on_unsubscribe = None
  554. self._on_disconnect = None
  555. self._on_socket_open = None
  556. self._on_socket_close = None
  557. self._on_socket_register_write = None
  558. self._on_socket_unregister_write = None
  559. self._websocket_path = "/mqtt"
  560. self._websocket_extra_headers = None
  561. # for clean_start == MQTT_CLEAN_START_FIRST_ONLY
  562. self._mqttv5_first_connect = True
  563. self.suppress_exceptions = False # For callbacks
  564. def __del__(self):
  565. self._reset_sockets()
  566. def _sock_recv(self, bufsize):
  567. try:
  568. return self._sock.recv(bufsize)
  569. except socket.error as err:
  570. if self._ssl and err.errno == ssl.SSL_ERROR_WANT_READ:
  571. raise WouldBlockError()
  572. if self._ssl and err.errno == ssl.SSL_ERROR_WANT_WRITE:
  573. self._call_socket_register_write()
  574. raise WouldBlockError()
  575. if err.errno == EAGAIN:
  576. raise WouldBlockError()
  577. raise
  578. def _sock_send(self, buf):
  579. try:
  580. return self._sock.send(buf)
  581. except socket.error as err:
  582. if self._ssl and err.errno == ssl.SSL_ERROR_WANT_READ:
  583. raise WouldBlockError()
  584. if self._ssl and err.errno == ssl.SSL_ERROR_WANT_WRITE:
  585. self._call_socket_register_write()
  586. raise WouldBlockError()
  587. if err.errno == EAGAIN:
  588. self._call_socket_register_write()
  589. raise WouldBlockError()
  590. raise
  591. def _sock_close(self):
  592. """Close the connection to the server."""
  593. if not self._sock:
  594. return
  595. try:
  596. sock = self._sock
  597. self._sock = None
  598. self._call_socket_unregister_write(sock)
  599. self._call_socket_close(sock)
  600. finally:
  601. # In case a callback fails, still close the socket to avoid leaking the file descriptor.
  602. sock.close()
  603. def _reset_sockets(self):
  604. self._sock_close()
  605. if self._sockpairR:
  606. self._sockpairR.close()
  607. self._sockpairR = None
  608. if self._sockpairW:
  609. self._sockpairW.close()
  610. self._sockpairW = None
  611. def reinitialise(self, client_id = "", clean_session = True, userdata = None):
  612. self._reset_sockets()
  613. self.__init__(client_id, clean_session, userdata)
  614. def ws_set_options(self, path = "/mqtt", headers = None):
  615. """ Set the path and headers for a websocket connection
  616. path is a string starting with / which should be the endpoint of the
  617. mqtt connection on the remote server
  618. headers can be either a dict or a callable object. If it is a dict then
  619. the extra items in the dict are added to the websocket headers. If it is
  620. a callable, then the default websocket headers are passed into this
  621. function and the result is used as the new headers.
  622. """
  623. self._websocket_path = path
  624. if headers is not None:
  625. if isinstance(headers, dict) or callable(headers):
  626. self._websocket_extra_headers = headers
  627. else:
  628. raise ValueError(
  629. "'headers' option to ws_set_options has to be either a dictionary or callable")
  630. def tls_set_context(self, context = None):
  631. """Configure network encryption and authentication context. Enables SSL/TLS support.
  632. context : an ssl.SSLContext object. By default this is given by
  633. `ssl.create_default_context()`, if available.
  634. Must be called before connect() or connect_async()."""
  635. if self._ssl_context is not None:
  636. raise ValueError('SSL/TLS has already been configured.')
  637. # Assume that have SSL support, or at least that context input behaves like ssl.SSLContext
  638. # in current versions of Python
  639. if context is None:
  640. if hasattr(ssl, 'create_default_context'):
  641. context = ssl.create_default_context()
  642. else:
  643. raise ValueError('SSL/TLS context must be specified')
  644. self._ssl = True
  645. self._ssl_context = context
  646. # Ensure _tls_insecure is consistent with check_hostname attribute
  647. if hasattr(context, 'check_hostname'):
  648. self._tls_insecure = not context.check_hostname
  649. def tls_set(self, ca_certs = None, certfile = None, keyfile = None, cert_reqs = None, tls_version = None,
  650. ciphers = None):
  651. """Configure network encryption and authentication options. Enables SSL/TLS support.
  652. ca_certs : a string path to the Certificate Authority certificate files
  653. that are to be treated as trusted by this client. If this is the only
  654. option given then the client will operate in a similar manner to a web
  655. browser. That is to say it will require the broker to have a
  656. certificate signed by the Certificate Authorities in ca_certs and will
  657. communicate using TLS v1, but will not attempt any form of
  658. authentication. This provides basic network encryption but may not be
  659. sufficient depending on how the broker is configured.
  660. By default, on Python 2.7.9+ or 3.4+, the default certification
  661. authority of the system is used. On older Python version this parameter
  662. is mandatory.
  663. certfile and keyfile are strings pointing to the PEM encoded client
  664. certificate and private keys respectively. If these arguments are not
  665. None then they will be used as client information for TLS based
  666. authentication. Support for this feature is broker dependent. Note
  667. that if either of these files in encrypted and needs a password to
  668. decrypt it, Python will ask for the password at the command line. It is
  669. not currently possible to define a callback to provide the password.
  670. cert_reqs allows the certificate requirements that the client imposes
  671. on the broker to be changed. By default this is ssl.CERT_REQUIRED,
  672. which means that the broker must provide a certificate. See the ssl
  673. pydoc for more information on this parameter.
  674. tls_version allows the version of the SSL/TLS protocol used to be
  675. specified. By default TLS v1 is used. Previous versions (all versions
  676. beginning with SSL) are possible but not recommended due to possible
  677. security problems.
  678. ciphers is a string specifying which encryption ciphers are allowable
  679. for this connection, or None to use the defaults. See the ssl pydoc for
  680. more information.
  681. Must be called before connect() or connect_async()."""
  682. if ssl is None:
  683. raise ValueError('This platform has no SSL/TLS.')
  684. if not hasattr(ssl, 'SSLContext'):
  685. # Require Python version that has SSL context support in standard library
  686. raise ValueError(
  687. 'Python 2.7.9 and 3.2 are the minimum supported versions for TLS.')
  688. if ca_certs is None and not hasattr(ssl.SSLContext, 'load_default_certs'):
  689. raise ValueError('ca_certs must not be None.')
  690. # Create SSLContext object
  691. if tls_version is None:
  692. tls_version = ssl.PROTOCOL_TLSv1
  693. # If the python version supports it, use highest TLS version automatically
  694. if hasattr(ssl, "PROTOCOL_TLS"):
  695. tls_version = ssl.PROTOCOL_TLS
  696. context = ssl.SSLContext(tls_version)
  697. # Configure context
  698. if certfile is not None:
  699. context.load_cert_chain(certfile, keyfile)
  700. if cert_reqs == ssl.CERT_NONE and hasattr(context, 'check_hostname'):
  701. context.check_hostname = False
  702. context.verify_mode = ssl.CERT_REQUIRED if cert_reqs is None else cert_reqs
  703. if ca_certs is not None:
  704. context.load_verify_locations(ca_certs)
  705. else:
  706. context.load_default_certs()
  707. if ciphers is not None:
  708. context.set_ciphers(ciphers)
  709. self.tls_set_context(context)
  710. if cert_reqs != ssl.CERT_NONE:
  711. # Default to secure, sets context.check_hostname attribute
  712. # if available
  713. self.tls_insecure_set(False)
  714. else:
  715. # But with ssl.CERT_NONE, we can not check_hostname
  716. self.tls_insecure_set(True)
  717. def tls_insecure_set(self, value):
  718. """Configure verification of the server hostname in the server certificate.
  719. If value is set to true, it is impossible to guarantee that the host
  720. you are connecting to is not impersonating your server. This can be
  721. useful in initial server testing, but makes it possible for a malicious
  722. third party to impersonate your server through DNS spoofing, for
  723. example.
  724. Do not use this function in a real system. Setting value to true means
  725. there is no point using encryption.
  726. Must be called before connect() and after either tls_set() or
  727. tls_set_context()."""
  728. if self._ssl_context is None:
  729. raise ValueError(
  730. 'Must configure SSL context before using tls_insecure_set.')
  731. self._tls_insecure = value
  732. # Ensure check_hostname is consistent with _tls_insecure attribute
  733. if hasattr(self._ssl_context, 'check_hostname'):
  734. # Rely on SSLContext to check host name
  735. # If verify_mode is CERT_NONE then the host name will never be checked
  736. self._ssl_context.check_hostname = not value
  737. def proxy_set(self, **proxy_args):
  738. """Configure proxying of MQTT connection. Enables support for SOCKS or
  739. HTTP proxies.
  740. Proxying is done through the PySocks library. Brief descriptions of the
  741. proxy_args parameters are below; see the PySocks docs for more info.
  742. (Required)
  743. proxy_type: One of {socks.HTTP, socks.SOCKS4, or socks.SOCKS5}
  744. proxy_addr: IP address or DNS name of proxy server
  745. (Optional)
  746. proxy_rdns: boolean indicating whether proxy lookup should be performed
  747. remotely (True, default) or locally (False)
  748. proxy_username: username for SOCKS5 proxy, or userid for SOCKS4 proxy
  749. proxy_password: password for SOCKS5 proxy
  750. Must be called before connect() or connect_async()."""
  751. if socks is None:
  752. raise ValueError("PySocks must be installed for proxy support.")
  753. elif not self._proxy_is_valid(proxy_args):
  754. raise ValueError("proxy_type and/or proxy_addr are invalid.")
  755. else:
  756. self._proxy = proxy_args
  757. def enable_logger(self, logger = None):
  758. """ Enables a logger to send log messages to """
  759. if logger is None:
  760. if self._logger is not None:
  761. # Do not replace existing logger
  762. return
  763. logger = logging.getLogger(__name__)
  764. self._logger = logger
  765. def disable_logger(self):
  766. self._logger = None
  767. def connect(self, host, port = 1883, keepalive = 60, bind_address = "", bind_port = 0,
  768. clean_start = MQTT_CLEAN_START_FIRST_ONLY, properties = None):
  769. """Connect to a remote broker.
  770. host is the hostname or IP address of the remote broker.
  771. port is the network port of the server host to connect to. Defaults to
  772. 1883. Note that the default port for MQTT over SSL/TLS is 8883 so if you
  773. are using tls_set() the port may need providing.
  774. keepalive: Maximum period in seconds between communications with the
  775. broker. If no other messages are being exchanged, this controls the
  776. rate at which the client will send ping messages to the broker.
  777. clean_start: (MQTT v5.0 only) True, False or MQTT_CLEAN_START_FIRST_ONLY.
  778. Sets the MQTT v5.0 clean_start flag always, never or on the first successful connect only,
  779. respectively. MQTT session data (such as outstanding messages and subscriptions)
  780. is cleared on successful connect when the clean_start flag is set.
  781. properties: (MQTT v5.0 only) the MQTT v5.0 properties to be sent in the
  782. MQTT connect packet.
  783. """
  784. if self._protocol == MQTTv5:
  785. self._mqttv5_first_connect = True
  786. else:
  787. if clean_start != MQTT_CLEAN_START_FIRST_ONLY:
  788. raise ValueError("Clean start only applies to MQTT V5")
  789. if properties != None:
  790. raise ValueError("Properties only apply to MQTT V5")
  791. self.connect_async(host, port, keepalive,
  792. bind_address, bind_port, clean_start, properties)
  793. return self.reconnect()
  794. def connect_srv(self, domain = None, keepalive = 60, bind_address = "",
  795. clean_start = MQTT_CLEAN_START_FIRST_ONLY, properties = None):
  796. """Connect to a remote broker.
  797. domain is the DNS domain to search for SRV records; if None,
  798. try to determine local domain name.
  799. keepalive, bind_address, clean_start and properties are as for connect()
  800. """
  801. if HAVE_DNS is False:
  802. raise ValueError(
  803. 'No DNS resolver library found, try "pip install dnspython" or "pip3 install dnspython3".')
  804. if domain is None:
  805. domain = socket.getfqdn()
  806. domain = domain[domain.find('.') + 1:]
  807. try:
  808. rr = '_mqtt._tcp.%s' % domain
  809. if self._ssl:
  810. # IANA specifies secure-mqtt (not mqtts) for port 8883
  811. rr = '_secure-mqtt._tcp.%s' % domain
  812. answers = []
  813. for answer in dns.resolver.query(rr, dns.rdatatype.SRV):
  814. addr = answer.target.to_text()[:-1]
  815. answers.append(
  816. (addr, answer.port, answer.priority, answer.weight))
  817. except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer, dns.resolver.NoNameservers):
  818. raise ValueError("No answer/NXDOMAIN for SRV in %s" % (domain))
  819. # FIXME: doesn't account for weight
  820. for answer in answers:
  821. host, port, prio, weight = answer
  822. try:
  823. return self.connect(host, port, keepalive, bind_address, clean_start, properties)
  824. except Exception:
  825. pass
  826. raise ValueError("No SRV hosts responded")
  827. def connect_async(self, host, port = 1883, keepalive = 60, bind_address = "", bind_port = 0,
  828. clean_start = MQTT_CLEAN_START_FIRST_ONLY, properties = None):
  829. """Connect to a remote broker asynchronously. This is a non-blocking
  830. connect call that can be used with loop_start() to provide very quick
  831. start.
  832. host is the hostname or IP address of the remote broker.
  833. port is the network port of the server host to connect to. Defaults to
  834. 1883. Note that the default port for MQTT over SSL/TLS is 8883 so if you
  835. are using tls_set() the port may need providing.
  836. keepalive: Maximum period in seconds between communications with the
  837. broker. If no other messages are being exchanged, this controls the
  838. rate at which the client will send ping messages to the broker.
  839. clean_start: (MQTT v5.0 only) True, False or MQTT_CLEAN_START_FIRST_ONLY.
  840. Sets the MQTT v5.0 clean_start flag always, never or on the first successful connect only,
  841. respectively. MQTT session data (such as outstanding messages and subscriptions)
  842. is cleared on successful connect when the clean_start flag is set.
  843. properties: (MQTT v5.0 only) the MQTT v5.0 properties to be sent in the
  844. MQTT connect packet. Use the Properties class.
  845. """
  846. if host is None or len(host) == 0:
  847. raise ValueError('Invalid host.')
  848. if port <= 0:
  849. raise ValueError('Invalid port number.')
  850. if keepalive < 0:
  851. raise ValueError('Keepalive must be >=0.')
  852. if bind_address != "" and bind_address is not None:
  853. if sys.version_info < (2, 7) or (3, 0) < sys.version_info < (3, 2):
  854. raise ValueError('bind_address requires Python 2.7 or 3.2.')
  855. if bind_port < 0:
  856. raise ValueError('Invalid bind port number.')
  857. self._host = host
  858. self._port = port
  859. self._keepalive = keepalive
  860. self._bind_address = bind_address
  861. self._bind_port = bind_port
  862. self._clean_start = clean_start
  863. self._connect_properties = properties
  864. self._state = mqtt_cs_connect_async
  865. def reconnect_delay_set(self, min_delay = 1, max_delay = 120):
  866. """ Configure the exponential reconnect delay
  867. When connection is lost, wait initially min_delay seconds and
  868. double this time every attempt. The wait is capped at max_delay.
  869. Once the client is fully connected (e.g. not only TCP socket, but
  870. received a success CONNACK), the wait timer is reset to min_delay.
  871. """
  872. with self._reconnect_delay_mutex:
  873. self._reconnect_min_delay = min_delay
  874. self._reconnect_max_delay = max_delay
  875. self._reconnect_delay = None
  876. def reconnect(self):
  877. """Reconnect the client after a disconnect. Can only be called after
  878. connect()/connect_async()."""
  879. if len(self._host) == 0:
  880. raise ValueError('Invalid host.')
  881. if self._port <= 0:
  882. raise ValueError('Invalid port number.')
  883. self._in_packet = {
  884. "command": 0,
  885. "have_remaining": 0,
  886. "remaining_count": [],
  887. "remaining_mult": 1,
  888. "remaining_length": 0,
  889. "packet": b"",
  890. "to_process": 0,
  891. "pos": 0}
  892. with self._out_packet_mutex:
  893. self._out_packet = collections.deque()
  894. with self._current_out_packet_mutex:
  895. self._current_out_packet = None
  896. with self._msgtime_mutex:
  897. self._last_msg_in = time_func()
  898. self._last_msg_out = time_func()
  899. self._ping_t = 0
  900. self._state = mqtt_cs_new
  901. self._sock_close()
  902. # Put messages in progress in a valid state.
  903. self._messages_reconnect_reset()
  904. sock = self._create_socket_connection()
  905. if self._ssl:
  906. # SSL is only supported when SSLContext is available (implies Python >= 2.7.9 or >= 3.2)
  907. verify_host = not self._tls_insecure
  908. try:
  909. # Try with server_hostname, even it's not supported in certain scenarios
  910. sock = self._ssl_context.wrap_socket(
  911. sock,
  912. server_hostname = self._host,
  913. do_handshake_on_connect = False,
  914. )
  915. except ssl.CertificateError:
  916. # CertificateError is derived from ValueError
  917. raise
  918. except ValueError:
  919. # Python version requires SNI in order to handle server_hostname, but SNI is not available
  920. sock = self._ssl_context.wrap_socket(
  921. sock,
  922. do_handshake_on_connect = False,
  923. )
  924. else:
  925. # If SSL context has already checked hostname, then don't need to do it again
  926. if (hasattr(self._ssl_context, 'check_hostname') and
  927. self._ssl_context.check_hostname):
  928. verify_host = False
  929. sock.settimeout(self._keepalive)
  930. sock.do_handshake()
  931. if verify_host:
  932. ssl.match_hostname(sock.getpeercert(), self._host)
  933. if self._transport == "websockets":
  934. sock.settimeout(self._keepalive)
  935. sock = WebsocketWrapper(sock, self._host, self._port, self._ssl,
  936. self._websocket_path, self._websocket_extra_headers)
  937. self._sock = sock
  938. self._sock.setblocking(0)
  939. self._registered_write = False
  940. self._call_socket_open()
  941. return self._send_connect(self._keepalive)
  942. def loop(self, timeout = 1.0, max_packets = 1):
  943. """Process network events.
  944. This function must be called regularly to ensure communication with the
  945. broker is carried out. It calls select() on the network socket to wait
  946. for network events. If incoming data is present it will then be
  947. processed. Outgoing commands, from e.g. publish(), are normally sent
  948. immediately that their function is called, but this is not always
  949. possible. loop() will also attempt to send any remaining outgoing
  950. messages, which also includes commands that are part of the flow for
  951. messages with QoS>0.
  952. timeout: The time in seconds to wait for incoming/outgoing network
  953. traffic before timing out and returning.
  954. max_packets: Not currently used.
  955. Returns MQTT_ERR_SUCCESS on success.
  956. Returns >0 on error.
  957. A ValueError will be raised if timeout < 0"""
  958. if timeout < 0.0:
  959. raise ValueError('Invalid timeout.')
  960. with self._current_out_packet_mutex:
  961. with self._out_packet_mutex:
  962. if self._current_out_packet is None and len(self._out_packet) > 0:
  963. self._current_out_packet = self._out_packet.popleft()
  964. if self._current_out_packet:
  965. wlist = [self._sock]
  966. else:
  967. wlist = []
  968. # used to check if there are any bytes left in the (SSL) socket
  969. pending_bytes = 0
  970. if hasattr(self._sock, 'pending'):
  971. pending_bytes = self._sock.pending()
  972. # if bytes are pending do not wait in select
  973. if pending_bytes > 0:
  974. timeout = 0.0
  975. # sockpairR is used to break out of select() before the timeout, on a
  976. # call to publish() etc.
  977. rlist = [self._sock, self._sockpairR]
  978. sock_r_ready = False
  979. sock_w_ready = False
  980. r_sock_ready = False
  981. if platform.system() == 'Windows':
  982. import select
  983. try:
  984. socklist = select.select(rlist, wlist, [], timeout)
  985. if self._sock in socklist[0]:
  986. sock_r_ready = True
  987. if self._sockpairR in socklist[0]:
  988. r_sock_ready = True
  989. sock_w_ready = True
  990. if self._sock in socklist[1]:
  991. sock_w_ready = True
  992. except TypeError as e:
  993. # Socket isn't correct type, in likelihood connection is lost
  994. self._easy_log(
  995. MQTT_LOG_DEBUG, "Caught exception in loop: %s", e)
  996. return MQTT_ERR_CONN_LOST
  997. except ValueError as e:
  998. # Can occur if we just reconnected but rlist/wlist contain a -1 for
  999. # some reason.
  1000. self._easy_log(
  1001. MQTT_LOG_DEBUG, "Caught exception in loop: %s", e)
  1002. return MQTT_ERR_CONN_LOST
  1003. except Exception as e:
  1004. # Note that KeyboardInterrupt, etc. can still terminate since they
  1005. # are not derived from Exception
  1006. self._easy_log(
  1007. MQTT_LOG_DEBUG, "Caught exception in loop: %s", e)
  1008. return MQTT_ERR_UNKNOWN
  1009. else:
  1010. import select
  1011. poller = select.poll()
  1012. if self._sock:
  1013. if self._sock in wlist:
  1014. poller.register(self._sock.fileno(), select.POLLIN | select.POLLOUT)
  1015. else:
  1016. poller.register(self._sock.fileno(), select.POLLIN)
  1017. if self._sockpairR:
  1018. poller.register(self._sockpairR.fileno(), select.POLLIN)
  1019. try:
  1020. events = poller.poll(1000 * timeout)
  1021. for fd, flag in events:
  1022. if self._sock and self._sock.fileno() == fd:
  1023. if flag & select.POLLIN:
  1024. sock_r_ready = True
  1025. if flag & select.POLLOUT:
  1026. sock_w_ready = True
  1027. if self._sockpairR and self._sockpairR.fileno() == fd:
  1028. if flag & select.POLLIN:
  1029. r_sock_ready = True
  1030. sock_w_ready = True
  1031. except TypeError as e:
  1032. self._easy_log(
  1033. MQTT_LOG_DEBUG, "Caught exception in loop: %s", e)
  1034. return MQTT_ERR_CONN_LOST
  1035. except ValueError as e:
  1036. self._easy_log(
  1037. MQTT_LOG_DEBUG, "Caught exception in loop: %s", e)
  1038. return MQTT_ERR_CONN_LOST
  1039. except Exception as e:
  1040. self._easy_log(
  1041. MQTT_LOG_DEBUG, "Caught exception in loop: %s", e)
  1042. return MQTT_ERR_UNKNOWN
  1043. finally:
  1044. if self._sock:
  1045. poller.unregister(self._sock.fileno())
  1046. if self._sockpairR:
  1047. poller.unregister(self._sockpairR.fileno())
  1048. # poller.close()
  1049. if sock_r_ready or pending_bytes > 0:
  1050. rc = self.loop_read(max_packets)
  1051. if rc or self._sock is None:
  1052. return rc
  1053. if r_sock_ready:
  1054. try:
  1055. self._sockpairR.recv(1)
  1056. except socket.error as err:
  1057. self._easy_log(
  1058. MQTT_LOG_DEBUG, "Caught exception in loop: %s", err)
  1059. if err.errno != EAGAIN:
  1060. raise
  1061. if sock_w_ready:
  1062. rc = self.loop_write(max_packets)
  1063. if rc or self._sock is None:
  1064. return rc
  1065. return self.loop_misc()
  1066. def publish(self, topic, payload = None, qos = 0, retain = False, properties = None):
  1067. """Publish a message on a topic.
  1068. This causes a message to be sent to the broker and subsequently from
  1069. the broker to any clients subscribing to matching topics.
  1070. topic: The topic that the message should be published on.
  1071. payload: The actual message to send. If not given, or set to None a
  1072. zero length message will be used. Passing an int or float will result
  1073. in the payload being converted to a string representing that number. If
  1074. you wish to send a true int/float, use struct.pack() to create the
  1075. payload you require.
  1076. qos: The quality of service level to use.
  1077. retain: If set to true, the message will be set as the "last known
  1078. good"/retained message for the topic.
  1079. properties: (MQTT v5.0 only) the MQTT v5.0 properties to be included.
  1080. Use the Properties class.
  1081. Returns a MQTTMessageInfo class, which can be used to determine whether
  1082. the message has been delivered (using info.is_published()) or to block
  1083. waiting for the message to be delivered (info.wait_for_publish()). The
  1084. message ID and return code of the publish() call can be found at
  1085. info.mid and info.rc.
  1086. For backwards compatibility, the MQTTMessageInfo class is iterable so
  1087. the old construct of (rc, mid) = client.publish(...) is still valid.
  1088. rc is MQTT_ERR_SUCCESS to indicate success or MQTT_ERR_NO_CONN if the
  1089. client is not currently connected. mid is the message ID for the
  1090. publish request. The mid value can be used to track the publish request
  1091. by checking against the mid argument in the on_publish() callback if it
  1092. is defined.
  1093. A ValueError will be raised if topic is None, has zero length or is
  1094. invalid (contains a wildcard), except if the MQTT version used is v5.0.
  1095. For v5.0, a zero length topic can be used when a Topic Alias has been set.
  1096. A ValueError will be raised if qos is not one of 0, 1 or 2, or if
  1097. the length of the payload is greater than 268435455 bytes."""
  1098. if self._protocol != MQTTv5:
  1099. if topic is None or len(topic) == 0:
  1100. raise ValueError('Invalid topic.')
  1101. topic = topic.encode('utf-8')
  1102. if self._topic_wildcard_len_check(topic) != MQTT_ERR_SUCCESS:
  1103. raise ValueError('Publish topic cannot contain wildcards.')
  1104. if qos < 0 or qos > 2:
  1105. raise ValueError('Invalid QoS level.')
  1106. if isinstance(payload, unicode):
  1107. local_payload = payload.encode('utf-8')
  1108. elif isinstance(payload, (bytes, bytearray)):
  1109. local_payload = payload
  1110. elif isinstance(payload, (int, float)):
  1111. local_payload = str(payload).encode('ascii')
  1112. elif payload is None:
  1113. local_payload = b''
  1114. else:
  1115. raise TypeError(
  1116. 'payload must be a string, bytearray, int, float or None.')
  1117. if len(local_payload) > 268435455:
  1118. raise ValueError('Payload too large.')
  1119. local_mid = self._mid_generate()
  1120. if qos == 0:
  1121. info = MQTTMessageInfo(local_mid)
  1122. rc = self._send_publish(
  1123. local_mid, topic, local_payload, qos, retain, False, info, properties)
  1124. info.rc = rc
  1125. return info
  1126. else:
  1127. message = MQTTMessage(local_mid, topic)
  1128. message.timestamp = time_func()
  1129. message.payload = local_payload
  1130. message.qos = qos
  1131. message.retain = retain
  1132. message.dup = False
  1133. message.properties = properties
  1134. with self._out_message_mutex:
  1135. if self._max_queued_messages > 0 and len(self._out_messages) >= self._max_queued_messages:
  1136. message.info.rc = MQTT_ERR_QUEUE_SIZE
  1137. return message.info
  1138. if local_mid in self._out_messages:
  1139. message.info.rc = MQTT_ERR_QUEUE_SIZE
  1140. return message.info
  1141. self._out_messages[message.mid] = message
  1142. if self._max_inflight_messages == 0 or self._inflight_messages < self._max_inflight_messages:
  1143. self._inflight_messages += 1
  1144. if qos == 1:
  1145. message.state = mqtt_ms_wait_for_puback
  1146. elif qos == 2:
  1147. message.state = mqtt_ms_wait_for_pubrec
  1148. rc = self._send_publish(message.mid, topic, message.payload, message.qos, message.retain,
  1149. message.dup, message.info, message.properties)
  1150. # remove from inflight messages so it will be send after a connection is made
  1151. if rc is MQTT_ERR_NO_CONN:
  1152. self._inflight_messages -= 1
  1153. message.state = mqtt_ms_publish
  1154. message.info.rc = rc
  1155. return message.info
  1156. else:
  1157. message.state = mqtt_ms_queued
  1158. message.info.rc = MQTT_ERR_SUCCESS
  1159. return message.info
  1160. def username_pw_set(self, username, password = None):
  1161. """Set a username and optionally a password for broker authentication.
  1162. Must be called before connect() to have any effect.
  1163. Requires a broker that supports MQTT v3.1.
  1164. username: The username to authenticate with. Need have no relationship to the client id. Must be unicode
  1165. [MQTT-3.1.3-11].
  1166. Set to None to reset client back to not using username/password for broker authentication.
  1167. password: The password to authenticate with. Optional, set to None if not required. If it is unicode, then it
  1168. will be encoded as UTF-8.
  1169. """
  1170. # [MQTT-3.1.3-11] User name must be UTF-8 encoded string
  1171. self._username = None if username is None else username.encode('utf-8')
  1172. self._password = password
  1173. if isinstance(self._password, unicode):
  1174. self._password = self._password.encode('utf-8')
  1175. def enable_bridge_mode(self):
  1176. """Sets the client in a bridge mode instead of client mode.
  1177. Must be called before connect() to have any effect.
  1178. Requires brokers that support bridge mode.
  1179. Under bridge mode, the broker will identify the client as a bridge and
  1180. not send it's own messages back to it. Hence a subsciption of # is
  1181. possible without message loops. This feature also correctly propagates
  1182. the retain flag on the messages.
  1183. Currently Mosquitto and RSMB support this feature. This feature can
  1184. be used to create a bridge between multiple broker.
  1185. """
  1186. self._client_mode = MQTT_BRIDGE
  1187. def is_connected(self):
  1188. """Returns the current status of the connection
  1189. True if connection exists
  1190. False if connection is closed
  1191. """
  1192. return self._state == mqtt_cs_connected
  1193. def disconnect(self, reasoncode = None, properties = None):
  1194. """Disconnect a connected client from the broker.
  1195. reasoncode: (MQTT v5.0 only) a ReasonCodes instance setting the MQTT v5.0
  1196. reasoncode to be sent with the disconnect. It is optional, the receiver
  1197. then assuming that 0 (success) is the value.
  1198. properties: (MQTT v5.0 only) a Properties instance setting the MQTT v5.0 properties
  1199. to be included. Optional - if not set, no properties are sent.
  1200. """
  1201. self._state = mqtt_cs_disconnecting
  1202. if self._sock is None:
  1203. return MQTT_ERR_NO_CONN
  1204. return self._send_disconnect(reasoncode, properties)
  1205. def subscribe(self, topic, qos = 0, options = None, properties = None):
  1206. """Subscribe the client to one or more topics.
  1207. This function may be called in three different ways (and a further three for MQTT v5.0):
  1208. Simple string and integer
  1209. -------------------------
  1210. e.g. subscribe("my/topic", 2)
  1211. topic: A string specifying the subscription topic to subscribe to.
  1212. qos: The desired quality of service level for the subscription.
  1213. Defaults to 0.
  1214. options and properties: Not used.
  1215. Simple string and subscribe options (MQTT v5.0 only)
  1216. ----------------------------------------------------
  1217. e.g. subscribe("my/topic", options=SubscribeOptions(qos=2))
  1218. topic: A string specifying the subscription topic to subscribe to.
  1219. qos: Not used.
  1220. options: The MQTT v5.0 subscribe options.
  1221. properties: a Properties instance setting the MQTT v5.0 properties
  1222. to be included. Optional - if not set, no properties are sent.
  1223. String and integer tuple
  1224. ------------------------
  1225. e.g. subscribe(("my/topic", 1))
  1226. topic: A tuple of (topic, qos). Both topic and qos must be present in
  1227. the tuple.
  1228. qos and options: Not used.
  1229. properties: Only used for MQTT v5.0. A Properties instance setting the
  1230. MQTT v5.0 properties. Optional - if not set, no properties are sent.
  1231. String and subscribe options tuple (MQTT v5.0 only)
  1232. ---------------------------------------------------
  1233. e.g. subscribe(("my/topic", SubscribeOptions(qos=1)))
  1234. topic: A tuple of (topic, SubscribeOptions). Both topic and subscribe
  1235. options must be present in the tuple.
  1236. qos and options: Not used.
  1237. properties: a Properties instance setting the MQTT v5.0 properties
  1238. to be included. Optional - if not set, no properties are sent.
  1239. List of string and integer tuples
  1240. ---------------------------------
  1241. e.g. subscribe([("my/topic", 0), ("another/topic", 2)])
  1242. This allows multiple topic subscriptions in a single SUBSCRIPTION
  1243. command, which is more efficient than using multiple calls to
  1244. subscribe().
  1245. topic: A list of tuple of format (topic, qos). Both topic and qos must
  1246. be present in all of the tuples.
  1247. qos, options and properties: Not used.
  1248. List of string and subscribe option tuples (MQTT v5.0 only)
  1249. -----------------------------------------------------------
  1250. e.g. subscribe([("my/topic", SubscribeOptions(qos=0), ("another/topic", SubscribeOptions(qos=2)])
  1251. This allows multiple topic subscriptions in a single SUBSCRIPTION
  1252. command, which is more efficient than using multiple calls to
  1253. subscribe().
  1254. topic: A list of tuple of format (topic, SubscribeOptions). Both topic and subscribe
  1255. options must be present in all of the tuples.
  1256. qos and options: Not used.
  1257. properties: a Properties instance setting the MQTT v5.0 properties
  1258. to be included. Optional - if not set, no properties are sent.
  1259. The function returns a tuple (result, mid), where result is
  1260. MQTT_ERR_SUCCESS to indicate success or (MQTT_ERR_NO_CONN, None) if the
  1261. client is not currently connected. mid is the message ID for the
  1262. subscribe request. The mid value can be used to track the subscribe
  1263. request by checking against the mid argument in the on_subscribe()
  1264. callback if it is defined.
  1265. Raises a ValueError if qos is not 0, 1 or 2, or if topic is None or has
  1266. zero string length, or if topic is not a string, tuple or list.
  1267. """
  1268. topic_qos_list = None
  1269. if isinstance(topic, tuple):
  1270. if self._protocol == MQTTv5:
  1271. topic, options = topic
  1272. if not isinstance(options, SubscribeOptions):
  1273. raise ValueError(
  1274. 'Subscribe options must be instance of SubscribeOptions class.')
  1275. else:
  1276. topic, qos = topic
  1277. if isinstance(topic, basestring):
  1278. if qos < 0 or qos > 2:
  1279. raise ValueError('Invalid QoS level.')
  1280. if self._protocol == MQTTv5:
  1281. if options == None:
  1282. # if no options are provided, use the QoS passed instead
  1283. options = SubscribeOptions(qos = qos)
  1284. elif qos != 0:
  1285. raise ValueError(
  1286. 'Subscribe options and qos parameters cannot be combined.')
  1287. if not isinstance(options, SubscribeOptions):
  1288. raise ValueError(
  1289. 'Subscribe options must be instance of SubscribeOptions class.')
  1290. topic_qos_list = [(topic.encode('utf-8'), options)]
  1291. else:
  1292. if topic is None or len(topic) == 0:
  1293. raise ValueError('Invalid topic.')
  1294. topic_qos_list = [(topic.encode('utf-8'), qos)]
  1295. elif isinstance(topic, list):
  1296. topic_qos_list = []
  1297. if self._protocol == MQTTv5:
  1298. for t, o in topic:
  1299. if not isinstance(o, SubscribeOptions):
  1300. # then the second value should be QoS
  1301. if o < 0 or o > 2:
  1302. raise ValueError('Invalid QoS level.')
  1303. o = SubscribeOptions(qos = o)
  1304. topic_qos_list.append((t.encode('utf-8'), o))
  1305. else:
  1306. for t, q in topic:
  1307. if q < 0 or q > 2:
  1308. raise ValueError('Invalid QoS level.')
  1309. if t is None or len(t) == 0 or not isinstance(t, basestring):
  1310. raise ValueError('Invalid topic.')
  1311. topic_qos_list.append((t.encode('utf-8'), q))
  1312. if topic_qos_list is None:
  1313. raise ValueError("No topic specified, or incorrect topic type.")
  1314. if any(self._filter_wildcard_len_check(topic) != MQTT_ERR_SUCCESS for topic, _ in topic_qos_list):
  1315. raise ValueError('Invalid subscription filter.')
  1316. if self._sock is None:
  1317. return (MQTT_ERR_NO_CONN, None)
  1318. return self._send_subscribe(False, topic_qos_list, properties)
  1319. def unsubscribe(self, topic, properties = None):
  1320. """Unsubscribe the client from one or more topics.
  1321. topic: A single string, or list of strings that are the subscription
  1322. topics to unsubscribe from.
  1323. properties: (MQTT v5.0 only) a Properties instance setting the MQTT v5.0 properties
  1324. to be included. Optional - if not set, no properties are sent.
  1325. Returns a tuple (result, mid), where result is MQTT_ERR_SUCCESS
  1326. to indicate success or (MQTT_ERR_NO_CONN, None) if the client is not
  1327. currently connected.
  1328. mid is the message ID for the unsubscribe request. The mid value can be
  1329. used to track the unsubscribe request by checking against the mid
  1330. argument in the on_unsubscribe() callback if it is defined.
  1331. Raises a ValueError if topic is None or has zero string length, or is
  1332. not a string or list.
  1333. """
  1334. topic_list = None
  1335. if topic is None:
  1336. raise ValueError('Invalid topic.')
  1337. if isinstance(topic, basestring):
  1338. if len(topic) == 0:
  1339. raise ValueError('Invalid topic.')
  1340. topic_list = [topic.encode('utf-8')]
  1341. elif isinstance(topic, list):
  1342. topic_list = []
  1343. for t in topic:
  1344. if len(t) == 0 or not isinstance(t, basestring):
  1345. raise ValueError('Invalid topic.')
  1346. topic_list.append(t.encode('utf-8'))
  1347. if topic_list is None:
  1348. raise ValueError("No topic specified, or incorrect topic type.")
  1349. if self._sock is None:
  1350. return (MQTT_ERR_NO_CONN, None)
  1351. return self._send_unsubscribe(False, topic_list, properties)
  1352. def loop_read(self, max_packets = 1):
  1353. """Process read network events. Use in place of calling loop() if you
  1354. wish to handle your client reads as part of your own application.
  1355. Use socket() to obtain the client socket to call select() or equivalent
  1356. on.
  1357. Do not use if you are using the threaded interface loop_start()."""
  1358. if self._sock is None:
  1359. return MQTT_ERR_NO_CONN
  1360. max_packets = len(self._out_messages) + len(self._in_messages)
  1361. if max_packets < 1:
  1362. max_packets = 1
  1363. for _ in range(0, max_packets):
  1364. if self._sock is None:
  1365. return MQTT_ERR_NO_CONN
  1366. rc = self._packet_read()
  1367. if rc > 0:
  1368. return self._loop_rc_handle(rc)
  1369. elif rc == MQTT_ERR_AGAIN:
  1370. return MQTT_ERR_SUCCESS
  1371. return MQTT_ERR_SUCCESS
  1372. def loop_write(self, max_packets = 1):
  1373. """Process write network events. Use in place of calling loop() if you
  1374. wish to handle your client writes as part of your own application.
  1375. Use socket() to obtain the client socket to call select() or equivalent
  1376. on.
  1377. Use want_write() to determine if there is data waiting to be written.
  1378. Do not use if you are using the threaded interface loop_start()."""
  1379. if self._sock is None:
  1380. return MQTT_ERR_NO_CONN
  1381. max_packets = len(self._out_packet) + 1
  1382. if max_packets < 1:
  1383. max_packets = 1
  1384. try:
  1385. for _ in range(0, max_packets):
  1386. rc = self._packet_write()
  1387. if rc > 0:
  1388. return self._loop_rc_handle(rc)
  1389. elif rc == MQTT_ERR_AGAIN:
  1390. return MQTT_ERR_SUCCESS
  1391. return MQTT_ERR_SUCCESS
  1392. finally:
  1393. if self.want_write():
  1394. self._call_socket_register_write()
  1395. else:
  1396. self._call_socket_unregister_write()
  1397. def want_write(self):
  1398. """Call to determine if there is network data waiting to be written.
  1399. Useful if you are calling select() yourself rather than using loop().
  1400. """
  1401. if self._current_out_packet or len(self._out_packet) > 0:
  1402. return True
  1403. else:
  1404. return False
  1405. def loop_misc(self):
  1406. """Process miscellaneous network events. Use in place of calling loop() if you
  1407. wish to call select() or equivalent on.
  1408. Do not use if you are using the threaded interface loop_start()."""
  1409. if self._sock is None:
  1410. return MQTT_ERR_NO_CONN
  1411. now = time_func()
  1412. self._check_keepalive()
  1413. if self._last_retry_check + 1 < now:
  1414. # Only check once a second at most
  1415. self._message_retry_check()
  1416. self._last_retry_check = now
  1417. if self._ping_t > 0 and now - self._ping_t >= self._keepalive:
  1418. # client->ping_t != 0 means we are waiting for a pingresp.
  1419. # This hasn't happened in the keepalive time so we should disconnect.
  1420. self._sock_close()
  1421. if self._state == mqtt_cs_disconnecting:
  1422. rc = MQTT_ERR_SUCCESS
  1423. else:
  1424. rc = 1
  1425. self._do_on_disconnect(rc)
  1426. return MQTT_ERR_CONN_LOST
  1427. return MQTT_ERR_SUCCESS
  1428. def max_inflight_messages_set(self, inflight):
  1429. """Set the maximum number of messages with QoS>0 that can be part way
  1430. through their network flow at once. Defaults to 20."""
  1431. if inflight < 0:
  1432. raise ValueError('Invalid inflight.')
  1433. self._max_inflight_messages = inflight
  1434. def max_queued_messages_set(self, queue_size):
  1435. """Set the maximum number of messages in the outgoing message queue.
  1436. 0 means unlimited."""
  1437. if queue_size < 0:
  1438. raise ValueError('Invalid queue size.')
  1439. if not isinstance(queue_size, int):
  1440. raise ValueError('Invalid type of queue size.')
  1441. self._max_queued_messages = queue_size
  1442. return self
  1443. def message_retry_set(self, retry):
  1444. """Set the timeout in seconds before a message with QoS>0 is retried.
  1445. 20 seconds by default."""
  1446. if retry < 0:
  1447. raise ValueError('Invalid retry.')
  1448. self._message_retry = retry
  1449. def user_data_set(self, userdata):
  1450. """Set the user data variable passed to callbacks. May be any data type."""
  1451. self._userdata = userdata
  1452. def will_set(self, topic, payload = None, qos = 0, retain = False, properties = None):
  1453. """Set a Will to be sent by the broker in case the client disconnects unexpectedly.
  1454. This must be called before connect() to have any effect.
  1455. topic: The topic that the will message should be published on.
  1456. payload: The message to send as a will. If not given, or set to None a
  1457. zero length message will be used as the will. Passing an int or float
  1458. will result in the payload being converted to a string representing
  1459. that number. If you wish to send a true int/float, use struct.pack() to
  1460. create the payload you require.
  1461. qos: The quality of service level to use for the will.
  1462. retain: If set to true, the will message will be set as the "last known
  1463. good"/retained message for the topic.
  1464. properties: (MQTT v5.0 only) a Properties instance setting the MQTT v5.0 properties
  1465. to be included with the will message. Optional - if not set, no properties are sent.
  1466. Raises a ValueError if qos is not 0, 1 or 2, or if topic is None or has
  1467. zero string length.
  1468. """
  1469. if topic is None or len(topic) == 0:
  1470. raise ValueError('Invalid topic.')
  1471. if qos < 0 or qos > 2:
  1472. raise ValueError('Invalid QoS level.')
  1473. if properties != None and not isinstance(properties, Properties):
  1474. raise ValueError(
  1475. "The properties argument must be an instance of the Properties class.")
  1476. if isinstance(payload, unicode):
  1477. self._will_payload = payload.encode('utf-8')
  1478. elif isinstance(payload, (bytes, bytearray)):
  1479. self._will_payload = payload
  1480. elif isinstance(payload, (int, float)):
  1481. self._will_payload = str(payload).encode('ascii')
  1482. elif payload is None:
  1483. self._will_payload = b""
  1484. else:
  1485. raise TypeError(
  1486. 'payload must be a string, bytearray, int, float or None.')
  1487. self._will = True
  1488. self._will_topic = topic.encode('utf-8')
  1489. self._will_qos = qos
  1490. self._will_retain = retain
  1491. self._will_properties = properties
  1492. def will_clear(self):
  1493. """ Removes a will that was previously configured with will_set().
  1494. Must be called before connect() to have any effect."""
  1495. self._will = False
  1496. self._will_topic = b""
  1497. self._will_payload = b""
  1498. self._will_qos = 0
  1499. self._will_retain = False
  1500. def socket(self):
  1501. """Return the socket or ssl object for this client."""
  1502. return self._sock
  1503. def loop_forever(self, timeout = 1.0, max_packets = 1, retry_first_connection = False):
  1504. """This function call loop() for you in an infinite blocking loop. It
  1505. is useful for the case where you only want to run the MQTT client loop
  1506. in your program.
  1507. loop_forever() will handle reconnecting for you. If you call
  1508. disconnect() in a callback it will return.
  1509. timeout: The time in seconds to wait for incoming/outgoing network
  1510. traffic before timing out and returning.
  1511. max_packets: Not currently used.
  1512. retry_first_connection: Should the first connection attempt be retried on failure.
  1513. Raises socket.error on first connection failures unless retry_first_connection=True
  1514. """
  1515. run = True
  1516. while run:
  1517. if self._thread_terminate is True:
  1518. break
  1519. if self._state == mqtt_cs_connect_async:
  1520. try:
  1521. self.reconnect()
  1522. except (socket.error, OSError, WebsocketConnectionError) as err:
  1523. if not retry_first_connection:
  1524. raise
  1525. self._easy_log(
  1526. MQTT_LOG_DEBUG, "Connection failed, retrying: %s", err)
  1527. self._reconnect_wait()
  1528. else:
  1529. break
  1530. while run:
  1531. rc = MQTT_ERR_SUCCESS
  1532. while rc == MQTT_ERR_SUCCESS:
  1533. rc = self.loop(timeout, max_packets)
  1534. # We don't need to worry about locking here, because we've
  1535. # either called loop_forever() when in single threaded mode, or
  1536. # in multi threaded mode when loop_stop() has been called and
  1537. # so no other threads can access _current_out_packet,
  1538. # _out_packet or _messages.
  1539. if (self._thread_terminate is True
  1540. and self._current_out_packet is None
  1541. and len(self._out_packet) == 0
  1542. and len(self._out_messages) == 0):
  1543. rc = 1
  1544. run = False
  1545. def should_exit():
  1546. return self._state == mqtt_cs_disconnecting or run is False or self._thread_terminate is True
  1547. if should_exit():
  1548. run = False
  1549. else:
  1550. self._reconnect_wait()
  1551. if should_exit():
  1552. run = False
  1553. else:
  1554. try:
  1555. self.reconnect()
  1556. except (socket.error, OSError, WebsocketConnectionError) as err:
  1557. self._easy_log(
  1558. MQTT_LOG_DEBUG, "Connection failed, retrying: %s", err)
  1559. return rc
  1560. def loop_start(self):
  1561. """This is part of the threaded client interface. Call this once to
  1562. start a new thread to process network traffic. This provides an
  1563. alternative to repeatedly calling loop() yourself.
  1564. """
  1565. if self._thread is not None:
  1566. return MQTT_ERR_INVAL
  1567. self._thread_terminate = False
  1568. self._thread = threading.Thread(target = self._thread_main)
  1569. self._thread.daemon = True
  1570. self._thread.start()
  1571. def loop_stop(self, force = False):
  1572. """This is part of the threaded client interface. Call this once to
  1573. stop the network thread previously created with loop_start(). This call
  1574. will block until the network thread finishes.
  1575. The force parameter is currently ignored.
  1576. """
  1577. if self._thread is None:
  1578. return MQTT_ERR_INVAL
  1579. self._thread_terminate = True
  1580. if threading.current_thread() != self._thread:
  1581. self._thread.join()
  1582. self._thread = None
  1583. @property
  1584. def on_log(self):
  1585. """If implemented, called when the client has log information.
  1586. Defined to allow debugging."""
  1587. return self._on_log
  1588. @on_log.setter
  1589. def on_log(self, func):
  1590. """ Define the logging callback implementation.
  1591. Expected signature is:
  1592. log_callback(client, userdata, level, buf)
  1593. client: the client instance for this callback
  1594. userdata: the private user data as set in Client() or userdata_set()
  1595. level: gives the severity of the message and will be one of
  1596. MQTT_LOG_INFO, MQTT_LOG_NOTICE, MQTT_LOG_WARNING,
  1597. MQTT_LOG_ERR, and MQTT_LOG_DEBUG.
  1598. buf: the message itself
  1599. """
  1600. self._on_log = func
  1601. @property
  1602. def on_connect(self):
  1603. """If implemented, called when the broker responds to our connection
  1604. request."""
  1605. return self._on_connect
  1606. @on_connect.setter
  1607. def on_connect(self, func):
  1608. """ Define the connect callback implementation.
  1609. Expected signature for MQTT v3.1 and v3.1.1 is:
  1610. connect_callback(client, userdata, flags, rc, properties=None)
  1611. and for MQTT v5.0:
  1612. connect_callback(client, userdata, flags, reasonCode, properties)
  1613. client: the client instance for this callback
  1614. userdata: the private user data as set in Client() or userdata_set()
  1615. flags: response flags sent by the broker
  1616. rc: the connection result
  1617. reasonCode: the MQTT v5.0 reason code: an instance of the ReasonCode class.
  1618. ReasonCode may be compared to interger.
  1619. properties: the MQTT v5.0 properties returned from the broker. An instance
  1620. of the Properties class.
  1621. For MQTT v3.1 and v3.1.1 properties is not provided but for compatibility
  1622. with MQTT v5.0, we recommand adding properties=None.
  1623. flags is a dict that contains response flags from the broker:
  1624. flags['session present'] - this flag is useful for clients that are
  1625. using clean session set to 0 only. If a client with clean
  1626. session=0, that reconnects to a broker that it has previously
  1627. connected to, this flag indicates whether the broker still has the
  1628. session information for the client. If 1, the session still exists.
  1629. The value of rc indicates success or not:
  1630. 0: Connection successful
  1631. 1: Connection refused - incorrect protocol version
  1632. 2: Connection refused - invalid client identifier
  1633. 3: Connection refused - server unavailable
  1634. 4: Connection refused - bad username or password
  1635. 5: Connection refused - not authorised
  1636. 6-255: Currently unused.
  1637. """
  1638. with self._callback_mutex:
  1639. self._on_connect = func
  1640. @property
  1641. def on_subscribe(self):
  1642. """If implemented, called when the broker responds to a subscribe
  1643. request."""
  1644. return self._on_subscribe
  1645. @on_subscribe.setter
  1646. def on_subscribe(self, func):
  1647. """ Define the suscribe callback implementation.
  1648. Expected signature for MQTT v3.1.1 and v3.1 is:
  1649. subscribe_callback(client, userdata, mid, granted_qos, properties=None)
  1650. and for MQTT v5.0:
  1651. subscribe_callback(client, userdata, mid, reasonCodes, properties)
  1652. client: the client instance for this callback
  1653. userdata: the private user data as set in Client() or userdata_set()
  1654. mid: matches the mid variable returned from the corresponding
  1655. subscribe() call.
  1656. granted_qos: list of integers that give the QoS level the broker has
  1657. granted for each of the different subscription requests.
  1658. reasonCodes: the MQTT v5.0 reason codes received from the broker for each
  1659. subscription. A list of ReasonCodes instances.
  1660. properties: the MQTT v5.0 properties received from the broker. A
  1661. list of Properties class instances.
  1662. """
  1663. with self._callback_mutex:
  1664. self._on_subscribe = func
  1665. @property
  1666. def on_message(self):
  1667. """If implemented, called when a message has been received on a topic
  1668. that the client subscribes to.
  1669. This callback will be called for every message received. Use
  1670. message_callback_add() to define multiple callbacks that will be called
  1671. for specific topic filters."""
  1672. return self._on_message
  1673. @on_message.setter
  1674. def on_message(self, func):
  1675. """ Define the message received callback implementation.
  1676. Expected signature is:
  1677. on_message_callback(client, userdata, message)
  1678. client: the client instance for this callback
  1679. userdata: the private user data as set in Client() or userdata_set()
  1680. message: an instance of MQTTMessage.
  1681. This is a class with members topic, payload, qos, retain.
  1682. """
  1683. with self._callback_mutex:
  1684. self._on_message = func
  1685. @property
  1686. def on_publish(self):
  1687. """If implemented, called when a message that was to be sent using the
  1688. publish() call has completed transmission to the broker.
  1689. For messages with QoS levels 1 and 2, this means that the appropriate
  1690. handshakes have completed. For QoS 0, this simply means that the message
  1691. has left the client.
  1692. This callback is important because even if the publish() call returns
  1693. success, it does not always mean that the message has been sent."""
  1694. return self._on_publish
  1695. @on_publish.setter
  1696. def on_publish(self, func):
  1697. """ Define the published message callback implementation.
  1698. Expected signature is:
  1699. on_publish_callback(client, userdata, mid)
  1700. client: the client instance for this callback
  1701. userdata: the private user data as set in Client() or userdata_set()
  1702. mid: matches the mid variable returned from the corresponding
  1703. publish() call, to allow outgoing messages to be tracked.
  1704. """
  1705. with self._callback_mutex:
  1706. self._on_publish = func
  1707. @property
  1708. def on_unsubscribe(self):
  1709. """If implemented, called when the broker responds to an unsubscribe
  1710. request."""
  1711. return self._on_unsubscribe
  1712. @on_unsubscribe.setter
  1713. def on_unsubscribe(self, func):
  1714. """ Define the unsubscribe callback implementation.
  1715. Expected signature for MQTT v3.1.1 and v3.1 is:
  1716. unsubscribe_callback(client, userdata, mid)
  1717. and for MQTT v5.0:
  1718. unsubscribe_callback(client, userdata, mid, properties, reasonCodes)
  1719. client: the client instance for this callback
  1720. userdata: the private user data as set in Client() or userdata_set()
  1721. mid: matches the mid variable returned from the corresponding
  1722. unsubscribe() call.
  1723. properties: the MQTT v5.0 properties received from the broker. A
  1724. list of Properties class instances.
  1725. reasonCodes: the MQTT v5.0 reason codes received from the broker for each
  1726. unsubscribe topic. A list of ReasonCodes instances
  1727. """
  1728. with self._callback_mutex:
  1729. self._on_unsubscribe = func
  1730. @property
  1731. def on_disconnect(self):
  1732. """If implemented, called when the client disconnects from the broker.
  1733. """
  1734. return self._on_disconnect
  1735. @on_disconnect.setter
  1736. def on_disconnect(self, func):
  1737. """ Define the disconnect callback implementation.
  1738. Expected signature for MQTT v3.1.1 and v3.1 is:
  1739. disconnect_callback(client, userdata, rc)
  1740. and for MQTT v5.0:
  1741. disconnect_callback(client, userdata, reasonCode, properties)
  1742. client: the client instance for this callback
  1743. userdata: the private user data as set in Client() or userdata_set()
  1744. rc: the disconnection result
  1745. The rc parameter indicates the disconnection state. If
  1746. MQTT_ERR_SUCCESS (0), the callback was called in response to
  1747. a disconnect() call. If any other value the disconnection
  1748. was unexpected, such as might be caused by a network error.
  1749. """
  1750. with self._callback_mutex:
  1751. self._on_disconnect = func
  1752. @property
  1753. def on_socket_open(self):
  1754. """If implemented, called just after the socket was opend."""
  1755. return self._on_socket_open
  1756. @on_socket_open.setter
  1757. def on_socket_open(self, func):
  1758. """Define the socket_open callback implementation.
  1759. This should be used to register the socket to an external event loop for reading.
  1760. Expected signature is:
  1761. socket_open_callback(client, userdata, socket)
  1762. client: the client instance for this callback
  1763. userdata: the private user data as set in Client() or userdata_set()
  1764. sock: the socket which was just opened.
  1765. """
  1766. with self._callback_mutex:
  1767. self._on_socket_open = func
  1768. def _call_socket_open(self):
  1769. """Call the socket_open callback with the just-opened socket"""
  1770. with self._callback_mutex:
  1771. if self.on_socket_open:
  1772. with self._in_callback_mutex:
  1773. try:
  1774. self.on_socket_open(self, self._userdata, self._sock)
  1775. except Exception as err:
  1776. self._easy_log(
  1777. MQTT_LOG_ERR, 'Caught exception in on_socket_open: %s', err)
  1778. if not self.suppress_exceptions:
  1779. raise
  1780. @property
  1781. def on_socket_close(self):
  1782. """If implemented, called just before the socket is closed."""
  1783. return self._on_socket_close
  1784. @on_socket_close.setter
  1785. def on_socket_close(self, func):
  1786. """Define the socket_close callback implementation.
  1787. This should be used to unregister the socket from an external event loop for reading.
  1788. Expected signature is:
  1789. socket_close_callback(client, userdata, socket)
  1790. client: the client instance for this callback
  1791. userdata: the private user data as set in Client() or userdata_set()
  1792. sock: the socket which is about to be closed.
  1793. """
  1794. with self._callback_mutex:
  1795. self._on_socket_close = func
  1796. def _call_socket_close(self, sock):
  1797. """Call the socket_close callback with the about-to-be-closed socket"""
  1798. with self._callback_mutex:
  1799. if self.on_socket_close:
  1800. with self._in_callback_mutex:
  1801. try:
  1802. self.on_socket_close(self, self._userdata, sock)
  1803. except Exception as err:
  1804. self._easy_log(
  1805. MQTT_LOG_ERR, 'Caught exception in on_socket_close: %s', err)
  1806. if not self.suppress_exceptions:
  1807. raise
  1808. @property
  1809. def on_socket_register_write(self):
  1810. """If implemented, called when the socket needs writing but can't."""
  1811. return self._on_socket_register_write
  1812. @on_socket_register_write.setter
  1813. def on_socket_register_write(self, func):
  1814. """Define the socket_register_write callback implementation.
  1815. This should be used to register the socket with an external event loop for writing.
  1816. Expected signature is:
  1817. socket_register_write_callback(client, userdata, socket)
  1818. client: the client instance for this callback
  1819. userdata: the private user data as set in Client() or userdata_set()
  1820. sock: the socket which should be registered for writing
  1821. """
  1822. with self._callback_mutex:
  1823. self._on_socket_register_write = func
  1824. def _call_socket_register_write(self):
  1825. """Call the socket_register_write callback with the unwritable socket"""
  1826. if not self._sock or self._registered_write:
  1827. return
  1828. self._registered_write = True
  1829. with self._callback_mutex:
  1830. if self.on_socket_register_write:
  1831. try:
  1832. self.on_socket_register_write(
  1833. self, self._userdata, self._sock)
  1834. except Exception as err:
  1835. self._easy_log(
  1836. MQTT_LOG_ERR, 'Caught exception in on_socket_register_write: %s', err)
  1837. if not self.suppress_exceptions:
  1838. raise
  1839. @property
  1840. def on_socket_unregister_write(self):
  1841. """If implemented, called when the socket doesn't need writing anymore."""
  1842. return self._on_socket_unregister_write
  1843. @on_socket_unregister_write.setter
  1844. def on_socket_unregister_write(self, func):
  1845. """Define the socket_unregister_write callback implementation.
  1846. This should be used to unregister the socket from an external event loop for writing.
  1847. Expected signature is:
  1848. socket_unregister_write_callback(client, userdata, socket)
  1849. client: the client instance for this callback
  1850. userdata: the private user data as set in Client() or userdata_set()
  1851. sock: the socket which should be unregistered for writing
  1852. """
  1853. with self._callback_mutex:
  1854. self._on_socket_unregister_write = func
  1855. def _call_socket_unregister_write(self, sock = None):
  1856. """Call the socket_unregister_write callback with the writable socket"""
  1857. sock = sock or self._sock
  1858. if not sock or not self._registered_write:
  1859. return
  1860. self._registered_write = False
  1861. with self._callback_mutex:
  1862. if self.on_socket_unregister_write:
  1863. try:
  1864. self.on_socket_unregister_write(self, self._userdata, sock)
  1865. except Exception as err:
  1866. self._easy_log(
  1867. MQTT_LOG_ERR, 'Caught exception in on_socket_unregister_write: %s', err)
  1868. if not self.suppress_exceptions:
  1869. raise
  1870. def message_callback_add(self, sub, callback):
  1871. """Register a message callback for a specific topic.
  1872. Messages that match 'sub' will be passed to 'callback'. Any
  1873. non-matching messages will be passed to the default on_message
  1874. callback.
  1875. Call multiple times with different 'sub' to define multiple topic
  1876. specific callbacks.
  1877. Topic specific callbacks may be removed with
  1878. message_callback_remove()."""
  1879. if callback is None or sub is None:
  1880. raise ValueError("sub and callback must both be defined.")
  1881. with self._callback_mutex:
  1882. self._on_message_filtered[sub] = callback
  1883. def message_callback_remove(self, sub):
  1884. """Remove a message callback previously registered with
  1885. message_callback_add()."""
  1886. if sub is None:
  1887. raise ValueError("sub must defined.")
  1888. with self._callback_mutex:
  1889. try:
  1890. del self._on_message_filtered[sub]
  1891. except KeyError: # no such subscription
  1892. pass
  1893. # ============================================================
  1894. # Private functions
  1895. # ============================================================
  1896. def _loop_rc_handle(self, rc, properties = None):
  1897. if rc:
  1898. self._sock_close()
  1899. if self._state == mqtt_cs_disconnecting:
  1900. rc = MQTT_ERR_SUCCESS
  1901. self._do_on_disconnect(rc, properties)
  1902. return rc
  1903. def _packet_read(self):
  1904. # This gets called if pselect() indicates that there is network data
  1905. # available - ie. at least one byte. What we do depends on what data we
  1906. # already have.
  1907. # If we've not got a command, attempt to read one and save it. This should
  1908. # always work because it's only a single byte.
  1909. # Then try to read the remaining length. This may fail because it is may
  1910. # be more than one byte - will need to save data pending next read if it
  1911. # does fail.
  1912. # Then try to read the remaining payload, where 'payload' here means the
  1913. # combined variable header and actual payload. This is the most likely to
  1914. # fail due to longer length, so save current data and current position.
  1915. # After all data is read, send to _mqtt_handle_packet() to deal with.
  1916. # Finally, free the memory and reset everything to starting conditions.
  1917. if self._in_packet['command'] == 0:
  1918. try:
  1919. command = self._sock_recv(1)
  1920. except WouldBlockError:
  1921. return MQTT_ERR_AGAIN
  1922. except socket.error as err:
  1923. self._easy_log(
  1924. MQTT_LOG_ERR, 'failed to receive on socket: %s', err)
  1925. return 1
  1926. else:
  1927. if len(command) == 0:
  1928. return 1
  1929. command, = struct.unpack("!B", command)
  1930. self._in_packet['command'] = command
  1931. if self._in_packet['have_remaining'] == 0:
  1932. # Read remaining
  1933. # Algorithm for decoding taken from pseudo code at
  1934. # http://publib.boulder.ibm.com/infocenter/wmbhelp/v6r0m0/topic/com.ibm.etools.mft.doc/ac10870_.htm
  1935. while True:
  1936. try:
  1937. byte = self._sock_recv(1)
  1938. except WouldBlockError:
  1939. return MQTT_ERR_AGAIN
  1940. except socket.error as err:
  1941. self._easy_log(
  1942. MQTT_LOG_ERR, 'failed to receive on socket: %s', err)
  1943. return 1
  1944. else:
  1945. if len(byte) == 0:
  1946. return 1
  1947. byte, = struct.unpack("!B", byte)
  1948. self._in_packet['remaining_count'].append(byte)
  1949. # Max 4 bytes length for remaining length as defined by protocol.
  1950. # Anything more likely means a broken/malicious client.
  1951. if len(self._in_packet['remaining_count']) > 4:
  1952. return MQTT_ERR_PROTOCOL
  1953. self._in_packet['remaining_length'] += (
  1954. byte & 127) * self._in_packet['remaining_mult']
  1955. self._in_packet['remaining_mult'] = self._in_packet['remaining_mult'] * 128
  1956. if (byte & 128) == 0:
  1957. break
  1958. self._in_packet['have_remaining'] = 1
  1959. self._in_packet['to_process'] = self._in_packet['remaining_length']
  1960. while self._in_packet['to_process'] > 0:
  1961. try:
  1962. data = self._sock_recv(self._in_packet['to_process'])
  1963. except WouldBlockError:
  1964. return MQTT_ERR_AGAIN
  1965. except socket.error as err:
  1966. self._easy_log(
  1967. MQTT_LOG_ERR, 'failed to receive on socket: %s', err)
  1968. return 1
  1969. else:
  1970. if len(data) == 0:
  1971. return 1
  1972. self._in_packet['to_process'] -= len(data)
  1973. self._in_packet['packet'] += data
  1974. # All data for this packet is read.
  1975. self._in_packet['pos'] = 0
  1976. rc = self._packet_handle()
  1977. # Free data and reset values
  1978. self._in_packet = {
  1979. 'command': 0,
  1980. 'have_remaining': 0,
  1981. 'remaining_count': [],
  1982. 'remaining_mult': 1,
  1983. 'remaining_length': 0,
  1984. 'packet': b"",
  1985. 'to_process': 0,
  1986. 'pos': 0}
  1987. with self._msgtime_mutex:
  1988. self._last_msg_in = time_func()
  1989. return rc
  1990. def _packet_write(self):
  1991. self._current_out_packet_mutex.acquire()
  1992. while self._current_out_packet:
  1993. packet = self._current_out_packet
  1994. try:
  1995. write_length = self._sock_send(
  1996. packet['packet'][packet['pos']:])
  1997. except (AttributeError, ValueError) as err:
  1998. self._current_out_packet_mutex.release()
  1999. self._easy_log(
  2000. MQTT_LOG_ERR, 'failed to send on socket: %s', err)
  2001. return MQTT_ERR_SUCCESS
  2002. except WouldBlockError:
  2003. self._current_out_packet_mutex.release()
  2004. return MQTT_ERR_AGAIN
  2005. except socket.error as err:
  2006. self._current_out_packet_mutex.release()
  2007. self._easy_log(
  2008. MQTT_LOG_ERR, 'failed to send on socket: %s', err)
  2009. return 1
  2010. if write_length > 0:
  2011. packet['to_process'] -= write_length
  2012. packet['pos'] += write_length
  2013. if packet['to_process'] == 0:
  2014. if (packet['command'] & 0xF0) == PUBLISH and packet['qos'] == 0:
  2015. with self._callback_mutex:
  2016. if self.on_publish:
  2017. with self._in_callback_mutex:
  2018. try:
  2019. self.on_publish(
  2020. self, self._userdata, packet['mid'])
  2021. except Exception as err:
  2022. self._easy_log(
  2023. MQTT_LOG_ERR, 'Caught exception in on_publish: %s', err)
  2024. if not self.suppress_exceptions:
  2025. raise
  2026. packet['info']._set_as_published()
  2027. if (packet['command'] & 0xF0) == DISCONNECT:
  2028. self._current_out_packet_mutex.release()
  2029. with self._msgtime_mutex:
  2030. self._last_msg_out = time_func()
  2031. self._do_on_disconnect(0)
  2032. self._sock_close()
  2033. return MQTT_ERR_SUCCESS
  2034. with self._out_packet_mutex:
  2035. if len(self._out_packet) > 0:
  2036. self._current_out_packet = self._out_packet.popleft()
  2037. else:
  2038. self._current_out_packet = None
  2039. else:
  2040. break
  2041. self._current_out_packet_mutex.release()
  2042. with self._msgtime_mutex:
  2043. self._last_msg_out = time_func()
  2044. return MQTT_ERR_SUCCESS
  2045. def _easy_log(self, level, fmt, *args):
  2046. if self.on_log is not None:
  2047. buf = fmt % args
  2048. try:
  2049. self.on_log(self, self._userdata, level, buf)
  2050. except Exception:
  2051. # Can't _easy_log this, as we'll recurse until we break
  2052. pass # self._logger will pick this up, so we're fine
  2053. if self._logger is not None:
  2054. level_std = LOGGING_LEVEL[level]
  2055. self._logger.log(level_std, fmt, *args)
  2056. def _check_keepalive(self):
  2057. if self._keepalive == 0:
  2058. return MQTT_ERR_SUCCESS
  2059. now = time_func()
  2060. with self._msgtime_mutex:
  2061. last_msg_out = self._last_msg_out
  2062. last_msg_in = self._last_msg_in
  2063. if self._sock is not None and (now - last_msg_out >= self._keepalive or now - last_msg_in >= self._keepalive):
  2064. if self._state == mqtt_cs_connected and self._ping_t == 0:
  2065. self._send_pingreq()
  2066. with self._msgtime_mutex:
  2067. self._last_msg_out = now
  2068. self._last_msg_in = now
  2069. else:
  2070. self._sock_close()
  2071. if self._state == mqtt_cs_disconnecting:
  2072. rc = MQTT_ERR_SUCCESS
  2073. else:
  2074. rc = 1
  2075. self._do_on_disconnect(rc)
  2076. def _mid_generate(self):
  2077. with self._mid_generate_mutex:
  2078. self._last_mid += 1
  2079. if self._last_mid == 65536:
  2080. self._last_mid = 1
  2081. return self._last_mid
  2082. @staticmethod
  2083. def _topic_wildcard_len_check(topic):
  2084. # Search for + or # in a topic. Return MQTT_ERR_INVAL if found.
  2085. # Also returns MQTT_ERR_INVAL if the topic string is too long.
  2086. # Returns MQTT_ERR_SUCCESS if everything is fine.
  2087. if b'+' in topic or b'#' in topic or len(topic) > 65535:
  2088. return MQTT_ERR_INVAL
  2089. else:
  2090. return MQTT_ERR_SUCCESS
  2091. @staticmethod
  2092. def _filter_wildcard_len_check(sub):
  2093. if (len(sub) == 0 or len(sub) > 65535
  2094. or any(b'+' in p or b'#' in p for p in sub.split(b'/') if len(p) > 1)
  2095. or b'#/' in sub):
  2096. return MQTT_ERR_INVAL
  2097. else:
  2098. return MQTT_ERR_SUCCESS
  2099. def _send_pingreq(self):
  2100. self._easy_log(MQTT_LOG_DEBUG, "Sending PINGREQ")
  2101. rc = self._send_simple_command(PINGREQ)
  2102. if rc == MQTT_ERR_SUCCESS:
  2103. self._ping_t = time_func()
  2104. return rc
  2105. def _send_pingresp(self):
  2106. self._easy_log(MQTT_LOG_DEBUG, "Sending PINGRESP")
  2107. return self._send_simple_command(PINGRESP)
  2108. def _send_puback(self, mid):
  2109. self._easy_log(MQTT_LOG_DEBUG, "Sending PUBACK (Mid: %d)", mid)
  2110. return self._send_command_with_mid(PUBACK, mid, False)
  2111. def _send_pubcomp(self, mid):
  2112. self._easy_log(MQTT_LOG_DEBUG, "Sending PUBCOMP (Mid: %d)", mid)
  2113. return self._send_command_with_mid(PUBCOMP, mid, False)
  2114. def _pack_remaining_length(self, packet, remaining_length):
  2115. remaining_bytes = []
  2116. while True:
  2117. byte = remaining_length % 128
  2118. remaining_length = remaining_length // 128
  2119. # If there are more digits to encode, set the top bit of this digit
  2120. if remaining_length > 0:
  2121. byte |= 0x80
  2122. remaining_bytes.append(byte)
  2123. packet.append(byte)
  2124. if remaining_length == 0:
  2125. # FIXME - this doesn't deal with incorrectly large payloads
  2126. return packet
  2127. def _pack_str16(self, packet, data):
  2128. if isinstance(data, unicode):
  2129. data = data.encode('utf-8')
  2130. packet.extend(struct.pack("!H", len(data)))
  2131. packet.extend(data)
  2132. def _send_publish(self, mid, topic, payload = b'', qos = 0, retain = False, dup = False, info = None,
  2133. properties = None):
  2134. # we assume that topic and payload are already properly encoded
  2135. assert not isinstance(topic, unicode) and not isinstance(
  2136. payload, unicode) and payload is not None
  2137. if self._sock is None:
  2138. return MQTT_ERR_NO_CONN
  2139. command = PUBLISH | ((dup & 0x1) << 3) | (qos << 1) | retain
  2140. packet = bytearray()
  2141. packet.append(command)
  2142. payloadlen = len(payload)
  2143. remaining_length = 2 + len(topic) + payloadlen
  2144. if payloadlen == 0:
  2145. if self._protocol == MQTTv5:
  2146. self._easy_log(
  2147. MQTT_LOG_DEBUG,
  2148. "Sending PUBLISH (d%d, q%d, r%d, m%d), '%s', properties=%s (NULL payload)",
  2149. dup, qos, retain, mid, topic, properties
  2150. )
  2151. else:
  2152. self._easy_log(
  2153. MQTT_LOG_DEBUG,
  2154. "Sending PUBLISH (d%d, q%d, r%d, m%d), '%s' (NULL payload)",
  2155. dup, qos, retain, mid, topic
  2156. )
  2157. else:
  2158. if self._protocol == MQTTv5:
  2159. self._easy_log(
  2160. MQTT_LOG_DEBUG,
  2161. "Sending PUBLISH (d%d, q%d, r%d, m%d), '%s', properties=%s, ... (%d bytes)",
  2162. dup, qos, retain, mid, topic, properties, payloadlen
  2163. )
  2164. else:
  2165. self._easy_log(
  2166. MQTT_LOG_DEBUG,
  2167. "Sending PUBLISH (d%d, q%d, r%d, m%d), '%s', ... (%d bytes)",
  2168. dup, qos, retain, mid, topic, payloadlen
  2169. )
  2170. if qos > 0:
  2171. # For message id
  2172. remaining_length += 2
  2173. if self._protocol == MQTTv5:
  2174. if properties == None:
  2175. packed_properties = b'\x00'
  2176. else:
  2177. packed_properties = properties.pack()
  2178. remaining_length += len(packed_properties)
  2179. self._pack_remaining_length(packet, remaining_length)
  2180. self._pack_str16(packet, topic)
  2181. if qos > 0:
  2182. # For message id
  2183. packet.extend(struct.pack("!H", mid))
  2184. if self._protocol == MQTTv5:
  2185. packet.extend(packed_properties)
  2186. packet.extend(payload)
  2187. return self._packet_queue(PUBLISH, packet, mid, qos, info)
  2188. def _send_pubrec(self, mid):
  2189. self._easy_log(MQTT_LOG_DEBUG, "Sending PUBREC (Mid: %d)", mid)
  2190. return self._send_command_with_mid(PUBREC, mid, False)
  2191. def _send_pubrel(self, mid):
  2192. self._easy_log(MQTT_LOG_DEBUG, "Sending PUBREL (Mid: %d)", mid)
  2193. return self._send_command_with_mid(PUBREL | 2, mid, False)
  2194. def _send_command_with_mid(self, command, mid, dup):
  2195. # For PUBACK, PUBCOMP, PUBREC, and PUBREL
  2196. if dup:
  2197. command |= 0x8
  2198. remaining_length = 2
  2199. packet = struct.pack('!BBH', command, remaining_length, mid)
  2200. return self._packet_queue(command, packet, mid, 1)
  2201. def _send_simple_command(self, command):
  2202. # For DISCONNECT, PINGREQ and PINGRESP
  2203. remaining_length = 0
  2204. packet = struct.pack('!BB', command, remaining_length)
  2205. return self._packet_queue(command, packet, 0, 0)
  2206. def _send_connect(self, keepalive):
  2207. proto_ver = self._protocol
  2208. # hard-coded UTF-8 encoded string
  2209. protocol = b"MQTT" if proto_ver >= MQTTv311 else b"MQIsdp"
  2210. remaining_length = 2 + len(protocol) + 1 + \
  2211. 1 + 2 + 2 + len(self._client_id)
  2212. connect_flags = 0
  2213. if self._protocol == MQTTv5:
  2214. if self._clean_start == True:
  2215. connect_flags |= 0x02
  2216. elif self._clean_start == MQTT_CLEAN_START_FIRST_ONLY and self._mqttv5_first_connect:
  2217. connect_flags |= 0x02
  2218. elif self._clean_session:
  2219. connect_flags |= 0x02
  2220. if self._will:
  2221. remaining_length += 2 + \
  2222. len(self._will_topic) + 2 + len(self._will_payload)
  2223. connect_flags |= 0x04 | ((self._will_qos & 0x03) << 3) | (
  2224. (self._will_retain & 0x01) << 5)
  2225. if self._username is not None:
  2226. remaining_length += 2 + len(self._username)
  2227. connect_flags |= 0x80
  2228. if self._password is not None:
  2229. connect_flags |= 0x40
  2230. remaining_length += 2 + len(self._password)
  2231. if self._protocol == MQTTv5:
  2232. if self._connect_properties == None:
  2233. packed_connect_properties = b'\x00'
  2234. else:
  2235. packed_connect_properties = self._connect_properties.pack()
  2236. remaining_length += len(packed_connect_properties)
  2237. if self._will:
  2238. if self._will_properties == None:
  2239. packed_will_properties = b'\x00'
  2240. else:
  2241. packed_will_properties = self._will_properties.pack()
  2242. remaining_length += len(packed_will_properties)
  2243. command = CONNECT
  2244. packet = bytearray()
  2245. packet.append(command)
  2246. # as per the mosquitto broker, if the MSB of this version is set
  2247. # to 1, then it treats the connection as a bridge
  2248. if self._client_mode == MQTT_BRIDGE:
  2249. proto_ver |= 0x80
  2250. self._pack_remaining_length(packet, remaining_length)
  2251. packet.extend(struct.pack("!H" + str(len(protocol)) + "sBBH", len(protocol), protocol, proto_ver, connect_flags,
  2252. keepalive))
  2253. if self._protocol == MQTTv5:
  2254. packet += packed_connect_properties
  2255. self._pack_str16(packet, self._client_id)
  2256. if self._will:
  2257. if self._protocol == MQTTv5:
  2258. packet += packed_will_properties
  2259. self._pack_str16(packet, self._will_topic)
  2260. self._pack_str16(packet, self._will_payload)
  2261. if self._username is not None:
  2262. self._pack_str16(packet, self._username)
  2263. if self._password is not None:
  2264. self._pack_str16(packet, self._password)
  2265. self._keepalive = keepalive
  2266. if self._protocol == MQTTv5:
  2267. self._easy_log(
  2268. MQTT_LOG_DEBUG,
  2269. "Sending CONNECT (u%d, p%d, wr%d, wq%d, wf%d, c%d, k%d) client_id=%s properties=%s",
  2270. (connect_flags & 0x80) >> 7,
  2271. (connect_flags & 0x40) >> 6,
  2272. (connect_flags & 0x20) >> 5,
  2273. (connect_flags & 0x18) >> 3,
  2274. (connect_flags & 0x4) >> 2,
  2275. (connect_flags & 0x2) >> 1,
  2276. keepalive,
  2277. self._client_id,
  2278. self._connect_properties
  2279. )
  2280. else:
  2281. self._easy_log(
  2282. MQTT_LOG_DEBUG,
  2283. "Sending CONNECT (u%d, p%d, wr%d, wq%d, wf%d, c%d, k%d) client_id=%s",
  2284. (connect_flags & 0x80) >> 7,
  2285. (connect_flags & 0x40) >> 6,
  2286. (connect_flags & 0x20) >> 5,
  2287. (connect_flags & 0x18) >> 3,
  2288. (connect_flags & 0x4) >> 2,
  2289. (connect_flags & 0x2) >> 1,
  2290. keepalive,
  2291. self._client_id
  2292. )
  2293. return self._packet_queue(command, packet, 0, 0)
  2294. def _send_disconnect(self, reasoncode = None, properties = None):
  2295. if self._protocol == MQTTv5:
  2296. self._easy_log(MQTT_LOG_DEBUG, "Sending DISCONNECT reasonCode=%s properties=%s",
  2297. reasoncode,
  2298. properties
  2299. )
  2300. else:
  2301. self._easy_log(MQTT_LOG_DEBUG, "Sending DISCONNECT")
  2302. remaining_length = 0
  2303. command = DISCONNECT
  2304. packet = bytearray()
  2305. packet.append(command)
  2306. if self._protocol == MQTTv5:
  2307. if properties != None or reasoncode != None:
  2308. if reasoncode == None:
  2309. reasoncode = ReasonCodes(DISCONNECT >> 4, identifier = 0)
  2310. remaining_length += 1
  2311. if properties != None:
  2312. packed_props = properties.pack()
  2313. remaining_length += len(packed_props)
  2314. self._pack_remaining_length(packet, remaining_length)
  2315. if self._protocol == MQTTv5:
  2316. if reasoncode != None:
  2317. packet += reasoncode.pack()
  2318. if properties != None:
  2319. packet += packed_props
  2320. return self._packet_queue(command, packet, 0, 0)
  2321. def _send_subscribe(self, dup, topics, properties = None):
  2322. remaining_length = 2
  2323. if self._protocol == MQTTv5:
  2324. if properties == None:
  2325. packed_subscribe_properties = b'\x00'
  2326. else:
  2327. packed_subscribe_properties = properties.pack()
  2328. remaining_length += len(packed_subscribe_properties)
  2329. for t, _ in topics:
  2330. remaining_length += 2 + len(t) + 1
  2331. command = SUBSCRIBE | (dup << 3) | 0x2
  2332. packet = bytearray()
  2333. packet.append(command)
  2334. self._pack_remaining_length(packet, remaining_length)
  2335. local_mid = self._mid_generate()
  2336. packet.extend(struct.pack("!H", local_mid))
  2337. if self._protocol == MQTTv5:
  2338. packet += packed_subscribe_properties
  2339. for t, q in topics:
  2340. self._pack_str16(packet, t)
  2341. if self._protocol == MQTTv5:
  2342. packet += q.pack()
  2343. else:
  2344. packet.append(q)
  2345. self._easy_log(
  2346. MQTT_LOG_DEBUG,
  2347. "Sending SUBSCRIBE (d%d, m%d) %s",
  2348. dup,
  2349. local_mid,
  2350. topics,
  2351. )
  2352. return (self._packet_queue(command, packet, local_mid, 1), local_mid)
  2353. def _send_unsubscribe(self, dup, topics, properties = None):
  2354. remaining_length = 2
  2355. if self._protocol == MQTTv5:
  2356. if properties == None:
  2357. packed_unsubscribe_properties = b'\x00'
  2358. else:
  2359. packed_unsubscribe_properties = properties.pack()
  2360. remaining_length += len(packed_unsubscribe_properties)
  2361. for t in topics:
  2362. remaining_length += 2 + len(t)
  2363. command = UNSUBSCRIBE | (dup << 3) | 0x2
  2364. packet = bytearray()
  2365. packet.append(command)
  2366. self._pack_remaining_length(packet, remaining_length)
  2367. local_mid = self._mid_generate()
  2368. packet.extend(struct.pack("!H", local_mid))
  2369. if self._protocol == MQTTv5:
  2370. packet += packed_unsubscribe_properties
  2371. for t in topics:
  2372. self._pack_str16(packet, t)
  2373. # topics_repr = ", ".join("'"+topic.decode('utf8')+"'" for topic in topics)
  2374. if self._protocol == MQTTv5:
  2375. self._easy_log(
  2376. MQTT_LOG_DEBUG,
  2377. "Sending UNSUBSCRIBE (d%d, m%d) %s %s",
  2378. dup,
  2379. local_mid,
  2380. properties,
  2381. topics,
  2382. )
  2383. else:
  2384. self._easy_log(
  2385. MQTT_LOG_DEBUG,
  2386. "Sending UNSUBSCRIBE (d%d, m%d) %s",
  2387. dup,
  2388. local_mid,
  2389. topics,
  2390. )
  2391. return (self._packet_queue(command, packet, local_mid, 1), local_mid)
  2392. def _message_retry_check_actual(self, messages, mutex):
  2393. with mutex:
  2394. now = time_func()
  2395. for m in messages.values():
  2396. if m.timestamp + self._message_retry < now:
  2397. if m.state == mqtt_ms_wait_for_puback or m.state == mqtt_ms_wait_for_pubrec:
  2398. m.timestamp = now
  2399. m.dup = True
  2400. self._send_publish(
  2401. m.mid,
  2402. m.topic.encode('utf-8'),
  2403. m.payload,
  2404. m.qos,
  2405. m.retain,
  2406. m.dup,
  2407. properties = m.properties,
  2408. )
  2409. elif m.state == mqtt_ms_wait_for_pubrel:
  2410. m.timestamp = now
  2411. self._send_pubrec(m.mid)
  2412. elif m.state == mqtt_ms_wait_for_pubcomp:
  2413. m.timestamp = now
  2414. self._send_pubrel(m.mid)
  2415. def _message_retry_check(self):
  2416. self._message_retry_check_actual(
  2417. self._out_messages, self._out_message_mutex)
  2418. self._message_retry_check_actual(
  2419. self._in_messages, self._in_message_mutex)
  2420. def _check_clean_session(self):
  2421. if self._protocol == MQTTv5:
  2422. if self._clean_start == MQTT_CLEAN_START_FIRST_ONLY:
  2423. return self._mqttv5_first_connect
  2424. else:
  2425. return self._clean_start
  2426. else:
  2427. return self._clean_session
  2428. def _messages_reconnect_reset_out(self):
  2429. with self._out_message_mutex:
  2430. self._inflight_messages = 0
  2431. for m in self._out_messages.values():
  2432. m.timestamp = 0
  2433. if self._max_inflight_messages == 0 or self._inflight_messages < self._max_inflight_messages:
  2434. if m.qos == 0:
  2435. m.state = mqtt_ms_publish
  2436. elif m.qos == 1:
  2437. # self._inflight_messages = self._inflight_messages + 1
  2438. if m.state == mqtt_ms_wait_for_puback:
  2439. m.dup = True
  2440. m.state = mqtt_ms_publish
  2441. elif m.qos == 2:
  2442. # self._inflight_messages = self._inflight_messages + 1
  2443. if self._check_clean_session():
  2444. if m.state != mqtt_ms_publish:
  2445. m.dup = True
  2446. m.state = mqtt_ms_publish
  2447. else:
  2448. if m.state == mqtt_ms_wait_for_pubcomp:
  2449. m.state = mqtt_ms_resend_pubrel
  2450. else:
  2451. if m.state == mqtt_ms_wait_for_pubrec:
  2452. m.dup = True
  2453. m.state = mqtt_ms_publish
  2454. else:
  2455. m.state = mqtt_ms_queued
  2456. def _messages_reconnect_reset_in(self):
  2457. with self._in_message_mutex:
  2458. if self._check_clean_session():
  2459. self._in_messages = collections.OrderedDict()
  2460. return
  2461. for m in self._in_messages.values():
  2462. m.timestamp = 0
  2463. if m.qos != 2:
  2464. self._in_messages.pop(m.mid)
  2465. else:
  2466. # Preserve current state
  2467. pass
  2468. def _messages_reconnect_reset(self):
  2469. self._messages_reconnect_reset_out()
  2470. self._messages_reconnect_reset_in()
  2471. def _packet_queue(self, command, packet, mid, qos, info = None):
  2472. mpkt = {
  2473. 'command': command,
  2474. 'mid': mid,
  2475. 'qos': qos,
  2476. 'pos': 0,
  2477. 'to_process': len(packet),
  2478. 'packet': packet,
  2479. 'info': info}
  2480. with self._out_packet_mutex:
  2481. self._out_packet.append(mpkt)
  2482. if self._current_out_packet_mutex.acquire(False):
  2483. if self._current_out_packet is None and len(self._out_packet) > 0:
  2484. self._current_out_packet = self._out_packet.popleft()
  2485. self._current_out_packet_mutex.release()
  2486. # Write a single byte to sockpairW (connected to sockpairR) to break
  2487. # out of select() if in threaded mode.
  2488. try:
  2489. self._sockpairW.send(sockpair_data)
  2490. except socket.error as err:
  2491. if err.errno != EAGAIN:
  2492. raise
  2493. if self._thread is None:
  2494. if self._in_callback_mutex.acquire(False):
  2495. self._in_callback_mutex.release()
  2496. return self.loop_write()
  2497. self._call_socket_register_write()
  2498. return MQTT_ERR_SUCCESS
  2499. def _packet_handle(self):
  2500. cmd = self._in_packet['command'] & 0xF0
  2501. if cmd == PINGREQ:
  2502. return self._handle_pingreq()
  2503. elif cmd == PINGRESP:
  2504. return self._handle_pingresp()
  2505. elif cmd == PUBACK:
  2506. return self._handle_pubackcomp("PUBACK")
  2507. elif cmd == PUBCOMP:
  2508. return self._handle_pubackcomp("PUBCOMP")
  2509. elif cmd == PUBLISH:
  2510. return self._handle_publish()
  2511. elif cmd == PUBREC:
  2512. return self._handle_pubrec()
  2513. elif cmd == PUBREL:
  2514. return self._handle_pubrel()
  2515. elif cmd == CONNACK:
  2516. return self._handle_connack()
  2517. elif cmd == SUBACK:
  2518. return self._handle_suback()
  2519. elif cmd == UNSUBACK:
  2520. return self._handle_unsuback()
  2521. elif cmd == DISCONNECT and self._protocol == MQTTv5: # only allowed in MQTT 5.0
  2522. return self._handle_disconnect()
  2523. else:
  2524. # If we don't recognise the command, return an error straight away.
  2525. self._easy_log(MQTT_LOG_ERR, "Error: Unrecognised command %s", cmd)
  2526. return MQTT_ERR_PROTOCOL
  2527. def _handle_pingreq(self):
  2528. if self._in_packet['remaining_length'] != 0:
  2529. return MQTT_ERR_PROTOCOL
  2530. self._easy_log(MQTT_LOG_DEBUG, "Received PINGREQ")
  2531. return self._send_pingresp()
  2532. def _handle_pingresp(self):
  2533. if self._in_packet['remaining_length'] != 0:
  2534. return MQTT_ERR_PROTOCOL
  2535. # No longer waiting for a PINGRESP.
  2536. self._ping_t = 0
  2537. self._easy_log(MQTT_LOG_DEBUG, "Received PINGRESP")
  2538. return MQTT_ERR_SUCCESS
  2539. def _handle_connack(self):
  2540. if self._protocol == MQTTv5:
  2541. self._easy_log(
  2542. MQTT_LOG_DEBUG,
  2543. "Received CONNACK, but not invalid protocol = %d",
  2544. MQTT_ERR_PROTOCOL
  2545. )
  2546. if self._in_packet['remaining_length'] < 2:
  2547. return MQTT_ERR_PROTOCOL
  2548. elif self._in_packet['remaining_length'] != 2:
  2549. self._easy_log(
  2550. MQTT_LOG_DEBUG,
  2551. "Received CONNACK, but not invalid protocol = %d",
  2552. MQTT_ERR_PROTOCOL
  2553. )
  2554. return MQTT_ERR_PROTOCOL
  2555. if self._protocol == MQTTv5:
  2556. (flags, result) = struct.unpack(
  2557. "!BB", self._in_packet['packet'][:2])
  2558. reason = ReasonCodes(CONNACK >> 4, identifier = result)
  2559. properties = Properties(CONNACK >> 4)
  2560. properties.unpack(self._in_packet['packet'][2:])
  2561. else:
  2562. (flags, result) = struct.unpack("!BB", self._in_packet['packet'])
  2563. if self._protocol == MQTTv311:
  2564. if result == CONNACK_REFUSED_PROTOCOL_VERSION:
  2565. self._easy_log(
  2566. MQTT_LOG_DEBUG,
  2567. "Received CONNACK (%s, %s), attempting downgrade to MQTT v3.1.",
  2568. flags, result
  2569. )
  2570. # Downgrade to MQTT v3.1
  2571. self._protocol = MQTTv31
  2572. return self.reconnect()
  2573. elif (result == CONNACK_REFUSED_IDENTIFIER_REJECTED
  2574. and self._client_id == b''):
  2575. self._easy_log(
  2576. MQTT_LOG_DEBUG,
  2577. "Received CONNACK (%s, %s), attempting to use non-empty CID",
  2578. flags, result,
  2579. )
  2580. self._client_id = base62(uuid.uuid4().int, padding = 22)
  2581. return self.reconnect()
  2582. if result == 0:
  2583. self._state = mqtt_cs_connected
  2584. self._reconnect_delay = None
  2585. if self._protocol == MQTTv5:
  2586. self._easy_log(
  2587. MQTT_LOG_DEBUG, "Received CONNACK (%s, %s) properties=%s", flags, reason, properties)
  2588. else:
  2589. self._easy_log(
  2590. MQTT_LOG_DEBUG, "Received CONNACK (%s, %s)", flags, result)
  2591. # it won't be the first successful connect any more
  2592. self._mqttv5_first_connect = False
  2593. with self._callback_mutex:
  2594. if self.on_connect:
  2595. flags_dict = {}
  2596. flags_dict['session present'] = flags & 0x01
  2597. with self._in_callback_mutex:
  2598. try:
  2599. if self._protocol == MQTTv5:
  2600. self.on_connect(self, self._userdata,
  2601. flags_dict, reason, properties)
  2602. else:
  2603. self.on_connect(
  2604. self, self._userdata, flags_dict, result)
  2605. except Exception as err:
  2606. self._easy_log(
  2607. MQTT_LOG_ERR, 'Caught exception in on_connect: %s', err)
  2608. if not self.suppress_exceptions:
  2609. raise
  2610. if result == 0:
  2611. rc = 0
  2612. with self._out_message_mutex:
  2613. for m in self._out_messages.values():
  2614. m.timestamp = time_func()
  2615. if m.state == mqtt_ms_queued:
  2616. self.loop_write() # Process outgoing messages that have just been queued up
  2617. return MQTT_ERR_SUCCESS
  2618. if m.qos == 0:
  2619. with self._in_callback_mutex: # Don't call loop_write after _send_publish()
  2620. rc = self._send_publish(
  2621. m.mid,
  2622. m.topic.encode('utf-8'),
  2623. m.payload,
  2624. m.qos,
  2625. m.retain,
  2626. m.dup,
  2627. properties = m.properties
  2628. )
  2629. if rc != 0:
  2630. return rc
  2631. elif m.qos == 1:
  2632. if m.state == mqtt_ms_publish:
  2633. self._inflight_messages += 1
  2634. m.state = mqtt_ms_wait_for_puback
  2635. with self._in_callback_mutex: # Don't call loop_write after _send_publish()
  2636. rc = self._send_publish(
  2637. m.mid,
  2638. m.topic.encode('utf-8'),
  2639. m.payload,
  2640. m.qos,
  2641. m.retain,
  2642. m.dup,
  2643. properties = m.properties
  2644. )
  2645. if rc != 0:
  2646. return rc
  2647. elif m.qos == 2:
  2648. if m.state == mqtt_ms_publish:
  2649. self._inflight_messages += 1
  2650. m.state = mqtt_ms_wait_for_pubrec
  2651. with self._in_callback_mutex: # Don't call loop_write after _send_publish()
  2652. rc = self._send_publish(
  2653. m.mid,
  2654. m.topic.encode('utf-8'),
  2655. m.payload,
  2656. m.qos,
  2657. m.retain,
  2658. m.dup,
  2659. properties = m.properties
  2660. )
  2661. if rc != 0:
  2662. return rc
  2663. elif m.state == mqtt_ms_resend_pubrel:
  2664. self._inflight_messages += 1
  2665. m.state = mqtt_ms_wait_for_pubcomp
  2666. with self._in_callback_mutex: # Don't call loop_write after _send_publish()
  2667. rc = self._send_pubrel(m.mid)
  2668. if rc != 0:
  2669. return rc
  2670. self.loop_write() # Process outgoing messages that have just been queued up
  2671. return rc
  2672. elif result > 0 and result < 6:
  2673. return MQTT_ERR_CONN_REFUSED
  2674. else:
  2675. return MQTT_ERR_PROTOCOL
  2676. def _handle_disconnect(self):
  2677. packet_type = DISCONNECT >> 4
  2678. reasonCode = properties = None
  2679. if self._in_packet['remaining_length'] > 2:
  2680. reasonCode = ReasonCodes(packet_type)
  2681. reasonCode.unpack(self._in_packet['packet'])
  2682. if self._in_packet['remaining_length'] > 3:
  2683. properties = Properties(packet_type)
  2684. props, props_len = properties.unpack(
  2685. self._in_packet['packet'][1:])
  2686. self._easy_log(MQTT_LOG_DEBUG, "Received DISCONNECT %s %s",
  2687. reasonCode,
  2688. properties
  2689. )
  2690. self._loop_rc_handle(reasonCode, properties)
  2691. return MQTT_ERR_SUCCESS
  2692. def _handle_suback(self):
  2693. self._easy_log(MQTT_LOG_DEBUG, "Received SUBACK")
  2694. pack_format = "!H" + str(len(self._in_packet['packet']) - 2) + 's'
  2695. (mid, packet) = struct.unpack(pack_format, self._in_packet['packet'])
  2696. if self._protocol == MQTTv5:
  2697. properties = Properties(SUBACK >> 4)
  2698. props, props_len = properties.unpack(packet)
  2699. reasoncodes = []
  2700. for c in packet[props_len:]:
  2701. if sys.version_info[0] < 3:
  2702. c = ord(c)
  2703. reasoncodes.append(ReasonCodes(SUBACK >> 4, identifier = c))
  2704. else:
  2705. pack_format = "!" + "B" * len(packet)
  2706. granted_qos = struct.unpack(pack_format, packet)
  2707. with self._callback_mutex:
  2708. if self.on_subscribe:
  2709. with self._in_callback_mutex: # Don't call loop_write after _send_publish()
  2710. try:
  2711. if self._protocol == MQTTv5:
  2712. self.on_subscribe(
  2713. self, self._userdata, mid, reasoncodes, properties)
  2714. else:
  2715. self.on_subscribe(
  2716. self, self._userdata, mid, granted_qos)
  2717. except Exception as err:
  2718. self._easy_log(
  2719. MQTT_LOG_ERR, 'Caught exception in on_subscribe: %s', err)
  2720. if not self.suppress_exceptions:
  2721. raise
  2722. return MQTT_ERR_SUCCESS
  2723. def _handle_publish(self):
  2724. rc = 0
  2725. header = self._in_packet['command']
  2726. message = MQTTMessage()
  2727. message.dup = (header & 0x08) >> 3
  2728. message.qos = (header & 0x06) >> 1
  2729. message.retain = (header & 0x01)
  2730. pack_format = "!H" + str(len(self._in_packet['packet']) - 2) + 's'
  2731. (slen, packet) = struct.unpack(pack_format, self._in_packet['packet'])
  2732. pack_format = '!' + str(slen) + 's' + str(len(packet) - slen) + 's'
  2733. (topic, packet) = struct.unpack(pack_format, packet)
  2734. if self._protocol != MQTTv5 and len(topic) == 0:
  2735. return MQTT_ERR_PROTOCOL
  2736. # Handle topics with invalid UTF-8
  2737. # This replaces an invalid topic with a message and the hex
  2738. # representation of the topic for logging. When the user attempts to
  2739. # access message.topic in the callback, an exception will be raised.
  2740. try:
  2741. print_topic = topic.decode('utf-8')
  2742. except UnicodeDecodeError:
  2743. print_topic = "TOPIC WITH INVALID UTF-8: " + str(topic)
  2744. message.topic = topic
  2745. if message.qos > 0:
  2746. pack_format = "!H" + str(len(packet) - 2) + 's'
  2747. (message.mid, packet) = struct.unpack(pack_format, packet)
  2748. if self._protocol == MQTTv5:
  2749. message.properties = Properties(PUBLISH >> 4)
  2750. props, props_len = message.properties.unpack(packet)
  2751. packet = packet[props_len:]
  2752. message.payload = packet
  2753. if self._protocol == MQTTv5:
  2754. self._easy_log(
  2755. MQTT_LOG_DEBUG,
  2756. "Received PUBLISH (d%d, q%d, r%d, m%d), '%s', properties=%s, ... (%d bytes)",
  2757. message.dup, message.qos, message.retain, message.mid,
  2758. print_topic, message.properties, len(message.payload)
  2759. )
  2760. else:
  2761. self._easy_log(
  2762. MQTT_LOG_DEBUG,
  2763. "Received PUBLISH (d%d, q%d, r%d, m%d), '%s', ... (%d bytes)",
  2764. message.dup, message.qos, message.retain, message.mid,
  2765. print_topic, len(message.payload)
  2766. )
  2767. message.timestamp = time_func()
  2768. if message.qos == 0:
  2769. self._handle_on_message(message)
  2770. return MQTT_ERR_SUCCESS
  2771. elif message.qos == 1:
  2772. rc = self._send_puback(message.mid)
  2773. self._handle_on_message(message)
  2774. return rc
  2775. elif message.qos == 2:
  2776. rc = self._send_pubrec(message.mid)
  2777. message.state = mqtt_ms_wait_for_pubrel
  2778. with self._in_message_mutex:
  2779. self._in_messages[message.mid] = message
  2780. return rc
  2781. else:
  2782. return MQTT_ERR_PROTOCOL
  2783. def _handle_pubrel(self):
  2784. if self._protocol == MQTTv5:
  2785. if self._in_packet['remaining_length'] < 2:
  2786. return MQTT_ERR_PROTOCOL
  2787. elif self._in_packet['remaining_length'] != 2:
  2788. return MQTT_ERR_PROTOCOL
  2789. mid, = struct.unpack("!H", self._in_packet['packet'])
  2790. self._easy_log(MQTT_LOG_DEBUG, "Received PUBREL (Mid: %d)", mid)
  2791. with self._in_message_mutex:
  2792. if mid in self._in_messages:
  2793. # Only pass the message on if we have removed it from the queue - this
  2794. # prevents multiple callbacks for the same message.
  2795. message = self._in_messages.pop(mid)
  2796. self._handle_on_message(message)
  2797. self._inflight_messages -= 1
  2798. if self._max_inflight_messages > 0:
  2799. with self._out_message_mutex:
  2800. rc = self._update_inflight()
  2801. if rc != MQTT_ERR_SUCCESS:
  2802. return rc
  2803. # FIXME: this should only be done if the message is known
  2804. # If unknown it's a protocol error and we should close the connection.
  2805. # But since we don't have (on disk) persistence for the session, it
  2806. # is possible that we must known about this message.
  2807. # Choose to acknwoledge this messsage (and thus losing a message) but
  2808. # avoid hanging. See #284.
  2809. return self._send_pubcomp(mid)
  2810. def _update_inflight(self):
  2811. # Dont lock message_mutex here
  2812. for m in self._out_messages.values():
  2813. if self._inflight_messages < self._max_inflight_messages:
  2814. if m.qos > 0 and m.state == mqtt_ms_queued:
  2815. self._inflight_messages += 1
  2816. if m.qos == 1:
  2817. m.state = mqtt_ms_wait_for_puback
  2818. elif m.qos == 2:
  2819. m.state = mqtt_ms_wait_for_pubrec
  2820. rc = self._send_publish(
  2821. m.mid,
  2822. m.topic.encode('utf-8'),
  2823. m.payload,
  2824. m.qos,
  2825. m.retain,
  2826. m.dup,
  2827. properties = m.properties,
  2828. )
  2829. if rc != 0:
  2830. return rc
  2831. else:
  2832. return MQTT_ERR_SUCCESS
  2833. return MQTT_ERR_SUCCESS
  2834. def _handle_pubrec(self):
  2835. if self._protocol == MQTTv5:
  2836. if self._in_packet['remaining_length'] < 2:
  2837. return MQTT_ERR_PROTOCOL
  2838. elif self._in_packet['remaining_length'] != 2:
  2839. return MQTT_ERR_PROTOCOL
  2840. mid, = struct.unpack("!H", self._in_packet['packet'][:2])
  2841. if self._protocol == MQTTv5:
  2842. if self._in_packet['remaining_length'] > 2:
  2843. reasonCode = ReasonCodes(PUBREC >> 4)
  2844. reasonCode.unpack(self._in_packet['packet'][2:])
  2845. if self._in_packet['remaining_length'] > 3:
  2846. properties = Properties(PUBREC >> 4)
  2847. props, props_len = properties.unpack(
  2848. self._in_packet['packet'][3:])
  2849. self._easy_log(MQTT_LOG_DEBUG, "Received PUBREC (Mid: %d)", mid)
  2850. with self._out_message_mutex:
  2851. if mid in self._out_messages:
  2852. msg = self._out_messages[mid]
  2853. msg.state = mqtt_ms_wait_for_pubcomp
  2854. msg.timestamp = time_func()
  2855. return self._send_pubrel(mid)
  2856. return MQTT_ERR_SUCCESS
  2857. def _handle_unsuback(self):
  2858. if self._protocol == MQTTv5:
  2859. if self._in_packet['remaining_length'] < 4:
  2860. return MQTT_ERR_PROTOCOL
  2861. elif self._in_packet['remaining_length'] != 2:
  2862. return MQTT_ERR_PROTOCOL
  2863. mid, = struct.unpack("!H", self._in_packet['packet'][:2])
  2864. if self._protocol == MQTTv5:
  2865. packet = self._in_packet['packet'][2:]
  2866. properties = Properties(UNSUBACK >> 4)
  2867. props, props_len = properties.unpack(packet)
  2868. reasoncodes = []
  2869. for c in packet[props_len:]:
  2870. if sys.version_info[0] < 3:
  2871. c = ord(c)
  2872. reasoncodes.append(ReasonCodes(UNSUBACK >> 4, identifier = c))
  2873. if len(reasoncodes) == 1:
  2874. reasoncodes = reasoncodes[0]
  2875. self._easy_log(MQTT_LOG_DEBUG, "Received UNSUBACK (Mid: %d)", mid)
  2876. with self._callback_mutex:
  2877. if self.on_unsubscribe:
  2878. with self._in_callback_mutex:
  2879. try:
  2880. if self._protocol == MQTTv5:
  2881. self.on_unsubscribe(
  2882. self, self._userdata, mid, properties, reasoncodes)
  2883. else:
  2884. self.on_unsubscribe(self, self._userdata, mid)
  2885. except Exception as err:
  2886. self._easy_log(
  2887. MQTT_LOG_ERR, 'Caught exception in on_unsubscribe: %s', err)
  2888. if not self.suppress_exceptions:
  2889. raise
  2890. return MQTT_ERR_SUCCESS
  2891. def _do_on_disconnect(self, rc, properties = None):
  2892. with self._callback_mutex:
  2893. if self.on_disconnect:
  2894. with self._in_callback_mutex:
  2895. try:
  2896. if properties:
  2897. self.on_disconnect(
  2898. self, self._userdata, rc, properties)
  2899. else:
  2900. self.on_disconnect(self, self._userdata, rc)
  2901. except Exception as err:
  2902. self._easy_log(
  2903. MQTT_LOG_ERR, 'Caught exception in on_disconnect: %s', err)
  2904. if not self.suppress_exceptions:
  2905. raise
  2906. def _do_on_publish(self, mid):
  2907. with self._callback_mutex:
  2908. if self.on_publish:
  2909. with self._in_callback_mutex:
  2910. try:
  2911. self.on_publish(self, self._userdata, mid)
  2912. except Exception as err:
  2913. self._easy_log(
  2914. MQTT_LOG_ERR, 'Caught exception in on_publish: %s', err)
  2915. if not self.suppress_exceptions:
  2916. raise
  2917. msg = self._out_messages.pop(mid)
  2918. msg.info._set_as_published()
  2919. if msg.qos > 0:
  2920. self._inflight_messages -= 1
  2921. if self._max_inflight_messages > 0:
  2922. rc = self._update_inflight()
  2923. if rc != MQTT_ERR_SUCCESS:
  2924. return rc
  2925. return MQTT_ERR_SUCCESS
  2926. def _handle_pubackcomp(self, cmd):
  2927. if self._protocol == MQTTv5:
  2928. if self._in_packet['remaining_length'] < 2:
  2929. return MQTT_ERR_PROTOCOL
  2930. elif self._in_packet['remaining_length'] != 2:
  2931. return MQTT_ERR_PROTOCOL
  2932. packet_type = PUBACK if cmd == "PUBACK" else PUBCOMP
  2933. packet_type = packet_type >> 4
  2934. mid, = struct.unpack("!H", self._in_packet['packet'][:2])
  2935. if self._protocol == MQTTv5:
  2936. if self._in_packet['remaining_length'] > 2:
  2937. reasonCode = ReasonCodes(packet_type)
  2938. reasonCode.unpack(self._in_packet['packet'][2:])
  2939. if self._in_packet['remaining_length'] > 3:
  2940. properties = Properties(packet_type)
  2941. props, props_len = properties.unpack(
  2942. self._in_packet['packet'][3:])
  2943. self._easy_log(MQTT_LOG_DEBUG, "Received %s (Mid: %d)", cmd, mid)
  2944. with self._out_message_mutex:
  2945. if mid in self._out_messages:
  2946. # Only inform the client the message has been sent once.
  2947. rc = self._do_on_publish(mid)
  2948. return rc
  2949. return MQTT_ERR_SUCCESS
  2950. def _handle_on_message(self, message):
  2951. matched = False
  2952. with self._callback_mutex:
  2953. try:
  2954. topic = message.topic
  2955. except UnicodeDecodeError:
  2956. topic = None
  2957. if topic is not None:
  2958. for callback in self._on_message_filtered.iter_match(message.topic):
  2959. with self._in_callback_mutex:
  2960. try:
  2961. callback(self, self._userdata, message)
  2962. except Exception as err:
  2963. self._easy_log(
  2964. MQTT_LOG_ERR,
  2965. 'Caught exception in user defined callback function %s: %s',
  2966. callback.__name__,
  2967. err
  2968. )
  2969. if not self.suppress_exceptions:
  2970. raise
  2971. matched = True
  2972. if matched == False and self.on_message:
  2973. with self._in_callback_mutex:
  2974. try:
  2975. self.on_message(self, self._userdata, message)
  2976. except Exception as err:
  2977. self._easy_log(
  2978. MQTT_LOG_ERR, 'Caught exception in on_message: %s', err)
  2979. if not self.suppress_exceptions:
  2980. raise
  2981. def _thread_main(self):
  2982. self.loop_forever(retry_first_connection = True)
  2983. def _reconnect_wait(self):
  2984. # See reconnect_delay_set for details
  2985. now = time_func()
  2986. with self._reconnect_delay_mutex:
  2987. if self._reconnect_delay is None:
  2988. self._reconnect_delay = self._reconnect_min_delay
  2989. else:
  2990. self._reconnect_delay = min(
  2991. self._reconnect_delay * 2,
  2992. self._reconnect_max_delay,
  2993. )
  2994. target_time = now + self._reconnect_delay
  2995. remaining = target_time - now
  2996. while (self._state != mqtt_cs_disconnecting
  2997. and not self._thread_terminate
  2998. and remaining > 0):
  2999. time.sleep(min(remaining, 1))
  3000. remaining = target_time - time_func()
  3001. @staticmethod
  3002. def _proxy_is_valid(p):
  3003. def check(t, a):
  3004. return (socks is not None and
  3005. t in set([socks.HTTP, socks.SOCKS4, socks.SOCKS5]) and a)
  3006. if isinstance(p, dict):
  3007. return check(p.get("proxy_type"), p.get("proxy_addr"))
  3008. elif isinstance(p, (list, tuple)):
  3009. return len(p) == 6 and check(p[0], p[1])
  3010. else:
  3011. return False
  3012. def _get_proxy(self):
  3013. if socks is None:
  3014. return None
  3015. # First, check if the user explicitly passed us a proxy to use
  3016. if self._proxy_is_valid(self._proxy):
  3017. return self._proxy
  3018. # Next, check for an mqtt_proxy environment variable as long as the host
  3019. # we're trying to connect to isn't listed under the no_proxy environment
  3020. # variable (matches built-in module urllib's behavior)
  3021. if not (hasattr(urllib_dot_request, "proxy_bypass") and
  3022. urllib_dot_request.proxy_bypass(self._host)):
  3023. env_proxies = urllib_dot_request.getproxies()
  3024. if "mqtt" in env_proxies:
  3025. parts = urllib_dot_parse.urlparse(env_proxies["mqtt"])
  3026. if parts.scheme == "http":
  3027. proxy = {
  3028. "proxy_type": socks.HTTP,
  3029. "proxy_addr": parts.hostname,
  3030. "proxy_port": parts.port
  3031. }
  3032. return proxy
  3033. elif parts.scheme == "socks":
  3034. proxy = {
  3035. "proxy_type": socks.SOCKS5,
  3036. "proxy_addr": parts.hostname,
  3037. "proxy_port": parts.port
  3038. }
  3039. return proxy
  3040. # Finally, check if the user has monkeypatched the PySocks library with
  3041. # a default proxy
  3042. socks_default = socks.get_default_proxy()
  3043. if self._proxy_is_valid(socks_default):
  3044. proxy_keys = ("proxy_type", "proxy_addr", "proxy_port",
  3045. "proxy_rdns", "proxy_username", "proxy_password")
  3046. return dict(zip(proxy_keys, socks_default))
  3047. # If we didn't find a proxy through any of the above methods, return
  3048. # None to indicate that the connection should be handled normally
  3049. return None
  3050. def _create_socket_connection(self):
  3051. proxy = self._get_proxy()
  3052. addr = (self._host, self._port)
  3053. source = (self._bind_address, self._bind_port)
  3054. if sys.version_info < (2, 7) or (3, 0) < sys.version_info < (3, 2):
  3055. # Have to short-circuit here because of unsupported source_address
  3056. # param in earlier Python versions.
  3057. return socket.create_connection(addr, timeout = self._keepalive)
  3058. if proxy:
  3059. return socks.create_connection(addr, source_address = source, timeout = self._keepalive, **proxy)
  3060. else:
  3061. return socket.create_connection(addr, source_address = source, timeout = self._keepalive)
  3062. # Compatibility class for easy porting from mosquitto.py.
  3063. class Mosquitto(Client):
  3064. def __init__(self, client_id = "", clean_session = True, userdata = None):
  3065. super(Mosquitto, self).__init__(client_id, clean_session, userdata)
  3066. class WebsocketWrapper(object):
  3067. OPCODE_CONTINUATION = 0x0
  3068. OPCODE_TEXT = 0x1
  3069. OPCODE_BINARY = 0x2
  3070. OPCODE_CONNCLOSE = 0x8
  3071. OPCODE_PING = 0x9
  3072. OPCODE_PONG = 0xa
  3073. def __init__(self, socket, host, port, is_ssl, path, extra_headers):
  3074. self.connected = False
  3075. self._ssl = is_ssl
  3076. self._host = host
  3077. self._port = port
  3078. self._socket = socket
  3079. self._path = path
  3080. self._sendbuffer = bytearray()
  3081. self._readbuffer = bytearray()
  3082. self._requested_size = 0
  3083. self._payload_head = 0
  3084. self._readbuffer_head = 0
  3085. self._do_handshake(extra_headers)
  3086. def __del__(self):
  3087. self._sendbuffer = None
  3088. self._readbuffer = None
  3089. def _do_handshake(self, extra_headers):
  3090. sec_websocket_key = uuid.uuid4().bytes
  3091. sec_websocket_key = base64.b64encode(sec_websocket_key)
  3092. websocket_headers = {
  3093. "Host": "{self._host:s}:{self._port:d}".format(self = self),
  3094. "Upgrade": "websocket",
  3095. "Connection": "Upgrade",
  3096. "Origin": "https://{self._host:s}:{self._port:d}".format(self = self),
  3097. "Sec-WebSocket-Key": sec_websocket_key.decode("utf8"),
  3098. "Sec-Websocket-Version": "13",
  3099. "Sec-Websocket-Protocol": "mqtt",
  3100. }
  3101. # This is checked in ws_set_options so it will either be None, a
  3102. # dictionary, or a callable
  3103. if isinstance(extra_headers, dict):
  3104. websocket_headers.update(extra_headers)
  3105. elif callable(extra_headers):
  3106. websocket_headers = extra_headers(websocket_headers)
  3107. header = "\r\n".join([
  3108. "GET {self._path} HTTP/1.1".format(self = self),
  3109. "\r\n".join("{}: {}".format(i, j)
  3110. for i, j in websocket_headers.items()),
  3111. "\r\n",
  3112. ]).encode("utf8")
  3113. self._socket.send(header)
  3114. has_secret = False
  3115. has_upgrade = False
  3116. while True:
  3117. # read HTTP response header as lines
  3118. byte = self._socket.recv(1)
  3119. self._readbuffer.extend(byte)
  3120. # line end
  3121. if byte == b"\n":
  3122. if len(self._readbuffer) > 2:
  3123. # check upgrade
  3124. if b"connection" in str(self._readbuffer).lower().encode('utf-8'):
  3125. if b"upgrade" not in str(self._readbuffer).lower().encode('utf-8'):
  3126. raise WebsocketConnectionError(
  3127. "WebSocket handshake error, connection not upgraded")
  3128. else:
  3129. has_upgrade = True
  3130. # check key hash
  3131. if b"sec-websocket-accept" in str(self._readbuffer).lower().encode('utf-8'):
  3132. GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
  3133. server_hash = self._readbuffer.decode(
  3134. 'utf-8').split(": ", 1)[1]
  3135. server_hash = server_hash.strip().encode('utf-8')
  3136. client_hash = sec_websocket_key.decode('utf-8') + GUID
  3137. client_hash = hashlib.sha1(client_hash.encode('utf-8'))
  3138. client_hash = base64.b64encode(client_hash.digest())
  3139. if server_hash != client_hash:
  3140. raise WebsocketConnectionError(
  3141. "WebSocket handshake error, invalid secret key")
  3142. else:
  3143. has_secret = True
  3144. else:
  3145. # ending linebreak
  3146. break
  3147. # reset linebuffer
  3148. self._readbuffer = bytearray()
  3149. # connection reset
  3150. elif not byte:
  3151. raise WebsocketConnectionError("WebSocket handshake error")
  3152. if not has_upgrade or not has_secret:
  3153. raise WebsocketConnectionError("WebSocket handshake error")
  3154. self._readbuffer = bytearray()
  3155. self.connected = True
  3156. def _create_frame(self, opcode, data, do_masking = 1):
  3157. header = bytearray()
  3158. length = len(data)
  3159. mask_key = bytearray(os.urandom(4))
  3160. mask_flag = do_masking
  3161. # 1 << 7 is the final flag, we don't send continuated data
  3162. header.append(1 << 7 | opcode)
  3163. if length < 126:
  3164. header.append(mask_flag << 7 | length)
  3165. elif length < 65536:
  3166. header.append(mask_flag << 7 | 126)
  3167. header += struct.pack("!H", length)
  3168. elif length < 0x8000000000000001:
  3169. header.append(mask_flag << 7 | 127)
  3170. header += struct.pack("!Q", length)
  3171. else:
  3172. raise ValueError("Maximum payload size is 2^63")
  3173. if mask_flag == 1:
  3174. for index in range(length):
  3175. data[index] ^= mask_key[index % 4]
  3176. data = mask_key + data
  3177. return header + data
  3178. def _buffered_read(self, length):
  3179. # try to recv and strore needed bytes
  3180. wanted_bytes = length - (len(self._readbuffer) - self._readbuffer_head)
  3181. if wanted_bytes > 0:
  3182. data = self._socket.recv(wanted_bytes)
  3183. if not data:
  3184. raise socket.error(errno.ECONNABORTED, 0)
  3185. else:
  3186. self._readbuffer.extend(data)
  3187. if len(data) < wanted_bytes:
  3188. raise socket.error(EAGAIN, 0)
  3189. self._readbuffer_head += length
  3190. return self._readbuffer[self._readbuffer_head - length:self._readbuffer_head]
  3191. def _recv_impl(self, length):
  3192. # try to decode websocket payload part from data
  3193. try:
  3194. self._readbuffer_head = 0
  3195. result = None
  3196. chunk_startindex = self._payload_head
  3197. chunk_endindex = self._payload_head + length
  3198. header1 = self._buffered_read(1)
  3199. header2 = self._buffered_read(1)
  3200. opcode = (header1[0] & 0x0f)
  3201. maskbit = (header2[0] & 0x80) == 0x80
  3202. lengthbits = (header2[0] & 0x7f)
  3203. payload_length = lengthbits
  3204. mask_key = None
  3205. # read length
  3206. if lengthbits == 0x7e:
  3207. value = self._buffered_read(2)
  3208. payload_length, = struct.unpack("!H", value)
  3209. elif lengthbits == 0x7f:
  3210. value = self._buffered_read(8)
  3211. payload_length, = struct.unpack("!Q", value)
  3212. # read mask
  3213. if maskbit:
  3214. mask_key = self._buffered_read(4)
  3215. # if frame payload is shorter than the requested data, read only the possible part
  3216. readindex = chunk_endindex
  3217. if payload_length < readindex:
  3218. readindex = payload_length
  3219. if readindex > 0:
  3220. # get payload chunk
  3221. payload = self._buffered_read(readindex)
  3222. # unmask only the needed part
  3223. if maskbit:
  3224. for index in range(chunk_startindex, readindex):
  3225. payload[index] ^= mask_key[index % 4]
  3226. result = payload[chunk_startindex:readindex]
  3227. self._payload_head = readindex
  3228. else:
  3229. payload = bytearray()
  3230. # check if full frame arrived and reset readbuffer and payloadhead if needed
  3231. if readindex == payload_length:
  3232. self._readbuffer = bytearray()
  3233. self._payload_head = 0
  3234. # respond to non-binary opcodes, their arrival is not guaranteed beacause of non-blocking sockets
  3235. if opcode == WebsocketWrapper.OPCODE_CONNCLOSE:
  3236. frame = self._create_frame(
  3237. WebsocketWrapper.OPCODE_CONNCLOSE, payload, 0)
  3238. self._socket.send(frame)
  3239. if opcode == WebsocketWrapper.OPCODE_PING:
  3240. frame = self._create_frame(
  3241. WebsocketWrapper.OPCODE_PONG, payload, 0)
  3242. self._socket.send(frame)
  3243. if opcode == WebsocketWrapper.OPCODE_BINARY and payload_length > 0:
  3244. return result
  3245. else:
  3246. raise socket.error(EAGAIN, 0)
  3247. except socket.error as err:
  3248. if err.errno == errno.ECONNABORTED:
  3249. self.connected = False
  3250. return b''
  3251. else:
  3252. # no more data
  3253. raise
  3254. def _send_impl(self, data):
  3255. # if previous frame was sent successfully
  3256. if len(self._sendbuffer) == 0:
  3257. # create websocket frame
  3258. frame = self._create_frame(
  3259. WebsocketWrapper.OPCODE_BINARY, bytearray(data))
  3260. self._sendbuffer.extend(frame)
  3261. self._requested_size = len(data)
  3262. # try to write out as much as possible
  3263. length = self._socket.send(self._sendbuffer)
  3264. self._sendbuffer = self._sendbuffer[length:]
  3265. if len(self._sendbuffer) == 0:
  3266. # buffer sent out completely, return with payload's size
  3267. return self._requested_size
  3268. else:
  3269. # couldn't send whole data, request the same data again with 0 as sent length
  3270. return 0
  3271. def recv(self, length):
  3272. return self._recv_impl(length)
  3273. def read(self, length):
  3274. return self._recv_impl(length)
  3275. def send(self, data):
  3276. return self._send_impl(data)
  3277. def write(self, data):
  3278. return self._send_impl(data)
  3279. def close(self):
  3280. self._socket.close()
  3281. def fileno(self):
  3282. return self._socket.fileno()
  3283. def pending(self):
  3284. # Fix for bug #131: a SSL socket may still have data available
  3285. # for reading without select() being aware of it.
  3286. if self._ssl:
  3287. return self._socket.pending()
  3288. else:
  3289. # normal socket rely only on select()
  3290. return 0
  3291. def setblocking(self, flag):
  3292. self._socket.setblocking(flag)