title: Unable to connect to windows shares
body: <p>I'm using the <a href="https://pypi.python.org/pypi/pysmb" rel="noreferrer">pysmb library</a> to query shares/directory structures on SMB/CIFS network shares.</p>
  <pre><code>def ListShares(Server, Username=None, Password=None, Domain=None):
      Ip = socket.gethostbyname(Server)
      conn = SMBConnection(Username,
                           Password,
                           'MyApp',
                           Server,
                           Domain,
                           use_ntlm_v2=True,
                           sign_options=SMBConnection.SIGN_WHEN_SUPPORTED,
                           is_direct_tcp=True)
      assert conn.connect(Ip)
  
      Response = conn.listShares(timeout=30)
  
      return [{'Name': x.name,
               'Type': x.type,
               'IsTemporary': x.isTemporary,
               'Comments': x.comments} for x in Response if not x.isSpecial]
  </code></pre>
  <p>When connecting to a linux box running samba, I can connect okay and everything works. When I try to connect to a Win7/SBS2008/Server2008 share, I get an error.</p>
  <p>If <code>is_direct_tcp=True</code>, I get an <code>Invalid protocol header for Direct TCP session message</code></p>
  <pre><code>File &quot;.../MyApp/Managers/SmbHelper.py&quot;, line 38, in ListShares assert conn.connect(Ip) 
  File &quot;/opt/pyenv/lib/python3.3/site-packages/smb/SMBConnection.py&quot;, line 111, in connect self._pollForNetBIOSPacket(timeout) 
  File &quot;/opt/pyenv/lib/python3.3/site-packages/smb/SMBConnection.py&quot;, line 504, in _pollForNetBIOSPacket self.feedData(data) 
  File &quot;/opt/pyenv/lib/python3.3/site-packages/nmb/base.py&quot;, line 49, in feedData length = self.data_nmb.decode(self.data_buf, offset) 
  File &quot;/opt/pyenv/lib/python3.3/site-packages/nmb/nmb_structs.py&quot;, line 60, in decode raise NMBError(&quot;Invalid protocol header for Direct TCP session message&quot;) 
  </code></pre>
  <p>If <code>is_direct_tcp=False</code>, I get a <code>NotConnectedError</code></p>
  <pre><code>File &quot;.../MyApp/Managers/SmbHelper.py&quot;, line 38, in ListShares assert conn.connect(Ip) 
  File &quot;/opt/pyenv/lib/python3.3/lib/site-packages/smb/SMBConnection.py&quot;, line 111, in connect self._pollForNetBIOSPacket(timeout) 
  File &quot;/opt/pyenv/lib/python3.3/lib/site-packages/smb/SMBConnection.py&quot;, line 466, in _pollForNetBIOSPacket raise NotConnectedError 
  </code></pre>
  <p>I'm hitting a bit of a brick wall. How can I work out what exactly is wrong and fix it?</p>
  <p>Further diagnostics...</p>
  <pre><code>smbclient -L linux.domain.local   -U MyUsername -W domain //Works
  smbclient -L linux.domain.local   -U MyUsername@domain    //Doesn't work (Auth failed)
  smbclient -L windows.domain.local -U MyUsername -W domain //Doesn't work (Auth failed)
  smbclient -L windows.domain.local -U MyUsername@domain    //Works
  
  smbclient -L [either].domain.local -U MyUsername@domain -W domain //Works, despite redundancy
  </code></pre>
  <p>So it seems Linux gets the domain from the <code>-W</code> parameter, Windows gets it from the <code>Username@Domain</code> syntax and providing both makes the <code>smbclient</code> call succeed to either server. Unfortunately, connecting to Windows doesn't succeed succeed from within pysmb even if I use the <code>@Domain</code> syntax</p>
  <h3>Solution</h3>
  <p>There were 3 problems... Firstly, when <code>use_direct_tcp</code> = <code>True</code>, <code>port</code> needs to be <code>445</code>. When it's <code>False</code>, port should be <code>139</code>. There was also a bug when using the module from Python3 (bytes were being incorrectly encoded). Finally, there was a problem with the way it was communicating with the server (at least when connecting to Windows boxes rather than a linux samba server).</p>
  <p>Michael Teo, author of the module has developed a fix which we've tested and works. He's planning to update the package shortly.</p>