miketeo/pysmb

TypeError: can't concat str to bytes

Chris-fullerton opened this issue · 2 comments

  • Env:
    macOS Ventura 13.0
    Apple M1, 2020

    conda list pysmb:

    # packages in environment at /Users/christopherchang/miniconda3/envs/facepcs:
    #
    # Name                    Version                   Build  Channel
    pysmb                     1.2.9.1                  pypi_0    pypi
    
    Python 3.10.11 (main, Apr 20 2023, 13:58:42) [Clang 14.0.6 ] on darwin
    
  • My Code:

def smb_transfer(self, export_file_name: str) -> None:
    conn = SMBConnection(
        SMB_ACCOUNT,
        SMB_PASSWORD,
        socket.gethostname(),
        SMB_HOST,
        is_direct_tcp=True,
    )
    result = conn.connect(SMB_HOST, 445)

    if result is True:
        self.logger.info("smb server login successful")
    else:
        self.logger.error("smb server login failed")
        return

    with open(combine_paths(self.log_path, export_file_name), "r") as export_file:
        conn.storeFile("card", export_file_name, export_file)
full exception stacks
Exception in thread Thread-2 (loop):
Traceback (most recent call last):
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/christopherchang/Work/FacePCS/python-package/facepcs/app/export_log.py", line 175, in loop
    self.smb_transfer(export_file_name)
  File "/Users/christopherchang/Work/FacePCS/python-package/facepcs/app/export_log.py", line 162, in smb_transfer
    conn.storeFile("card", export_file_name, export_file)
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/site-packages/smb/SMBConnection.py", line 376, in storeFile
    return self.storeFileFromOffset(service_name, path, file_obj, 0, True, timeout, show_progress = show_progress, tqdm_kwargs = tqdm_kwargs)
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/site-packages/smb/SMBConnection.py", line 408, in storeFileFromOffset
    self._pollForNetBIOSPacket(timeout)
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/site-packages/smb/SMBConnection.py", line 649, in _pollForNetBIOSPacket
    self.feedData(data)
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/site-packages/nmb/base.py", line 54, in feedData
    self._processNMBSessionPacket(self.data_nmb)
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/site-packages/nmb/base.py", line 75, in _processNMBSessionPacket
    self.onNMBSessionMessage(packet.flags, packet.data)
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/site-packages/smb/base.py", line 150, in onNMBSessionMessage
    if self._updateState(self.smb_message):
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/site-packages/smb/base.py", line 344, in _updateState_SMB2
    req.callback(message, **req.kwargs)
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/site-packages/smb/base.py", line 1105, in createCB
    sendWrite(create_message.tid, create_message.payload.fid, starting_offset)
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/site-packages/smb/base.py", line 1116, in sendWrite
    self._sendSMBMessage(m)
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/site-packages/smb/base.py", line 236, in _sendSMBMessage_SMB2
    raw_data = smb_message.encode()
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/site-packages/smb/smb2_structs.py", line 79, in encode
    self.payload.prepare(self)
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/site-packages/smb/smb2_structs.py", line 465, in prepare
    message.data = struct.pack(self.STRUCTURE_FORMAT,
TypeError: can't concat str to bytes
  ...
  File "/Users/christopherchang/miniconda3/envs/facepcs/lib/python3.10/site-packages/smb/smb2_structs.py", line 465, in prepare
    message.data = struct.pack(self.STRUCTURE_FORMAT,
TypeError: can't concat str to bytes

Have you tried opening the file in binary mode?
open(combine_paths(self.log_path, export_file_name), "rb")

Have you tried opening the file in binary mode? open(combine_paths(self.log_path, export_file_name), "rb")

Solved, this works for me 😄.

@miketeo do you think it is necessary to update the exception message for this scenario? If you don't think it's necessary, you can close this issues at any time, because my problem has been solved.