question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: startswith first arg must be bytes or a tuple of bytes, not str

See original GitHub issue

python 3, winrm version 0.2

code

command = 'mkdir C:/a/b/c'
s = winrm.Session(hostname, auth=(username, password))
r = s.run_ps(command)

If command is ‘ls C:/a/b’ ,then success.

Traceback

Traceback (most recent call last):
  File "/usr/lib64/python3.4/site-packages/tornado/web.py", line 1443, in _execute
    result = method(*self.path_args, **self.path_kwargs)
  File "/g/ops_card-master/handler/business/script.py", line 40, in post
    result = self.exec_script(pk)
  File "/g/ops_card-master/handler/decorator.py", line 4, in wrapper
    result = func(*args, **kwargs)
  File "/g/ops_card-master/handler/business/script.py", line 136, in exec_script
    result = self.pool.starmap(self.exe, li)
  File "/usr/lib64/python3.4/multiprocessing/pool.py", line 268, in starmap
    return self._map_async(func, iterable, starmapstar, chunksize).get()
  File "/usr/lib64/python3.4/multiprocessing/pool.py", line 599, in get
    raise self._value
  File "/usr/lib64/python3.4/multiprocessing/pool.py", line 119, in worker
    result = (True, func(*args, **kwds))
  File "/usr/lib64/python3.4/multiprocessing/pool.py", line 47, in starmapstar
    return list(itertools.starmap(args[0], args[1]))
  File "/g/ops_card-master/handler/decorator.py", line 18, in wrapper
    result = func(*args, **kwargs)
  File "/g/ops_card-master/handler/business/script.py", line 172, in exe
    r = s.run_ps(command)
  File "/usr/lib/python3.4/site-packages/pywinrm-0.2.0-py3.4.egg/winrm/__init__.py", line 54, in run_ps
    rs.std_err = self._clean_error_msg(rs.std_err)
  File "/usr/lib/python3.4/site-packages/pywinrm-0.2.0-py3.4.egg/winrm/__init__.py", line 62, in _clean_error_msg
    if msg.startswith("#< CLIXML\r\n"):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:9
  • Comments:8

github_iconTop GitHub Comments

9reactions
bikerider262commented, Aug 11, 2016

I hit the same issue, but my tweak was a little different:

I am on python3.4 and version 0.2.0

import winrm

s = winrm.Session('hostname', auth=('administrator', 'password'))

command = """(Get-NetIPAddress).IPAddress | % { if ($_ -match "^10.") {write-host $_} }"""

r = s.run_ps(command)

print(r.std_out.decode('utf-8'))

Output:

… …py_projects/winrm_ps_basic.py", line 7, in <module> r = s.run_ps(command) File “/usr/local/lib/python3.4/site-packages/winrm/init.py”, line 55, in run_ps rs.std_err = self._clean_error_msg(rs.std_err) File “/usr/local/lib/python3.4/site-packages/winrm/init.py”, line 63, in _clean_error_msg if msg.startswith(“#< CLIXML\r\n”): TypeError: startswith first arg must be bytes or a tuple of bytes, not str

Changing code as suggested above gave me this message:

Warning: there was a problem converting the Powershell error message: can’t use a string pattern on a bytes-like object 192.168.33.32

Process finished with exit code 0

So… warning looked suspicious…

I removed the b

I changed the message before it went into def _clean_error_msg(self, msg):

I changed rs.std_err in def run_ps(self, script):

from: rs.std_err = self._clean_error_msg(rs.std_err) to: rs.std_err = self._clean_error_msg(rs.std_err.decode(‘utf-8’))

I now have a clean output.

Hope this helps.

7reactions
JPvRielcommented, Feb 7, 2017

If anyone is looking for a quick monkey patch based on @bikerider262’s solution, this worked for me

def fix_run_ps(self, script):
    from base64 import b64encode
    encoded_ps = b64encode(script.encode('utf_16_le')).decode('ascii')
    rs = self.run_cmd('powershell -encodedcommand {0}'.format(encoded_ps))
    if len(rs.std_err):
        rs.std_err = self._clean_error_msg(rs.std_err.decode('utf-8'))
    return rs

winrm.Session.run_ps = fix_run_ps
Read more comments on GitHub >

github_iconTop Results From Across the Web

startswith TypeError in function - python - Stack Overflow
It's because you're opening the file in bytes mode, and so you're calling bytes.startswith() and not str.startswith() . You need to do line.startswith(b'>') ......
Read more >
How to Solve startswith first arg must be str or a tuple of str, not ...
If we read the TypeError: startswith first arg must be str or a tuple of str, not (bool/list/int) error carefully, we will see...
Read more >
Diff. between Py 2.7 and 3 - Python Forum
TypeError : startswith first arg must be bytes or a tuple of bytes, not str ... str.startswith(str, beg = 0,end = len(string)); so,...
Read more >
Bug #22816: TypeError: startswith first arg must be bytes or a ...
Thus, the sentence at osd.py:375 ( if line. startswith('Disk /'): ) fails because we are trying a string against startswith .
Read more >
24884 (Migrations created in Python2.7 fail in Python3.4)
... line 98, in quote_name if name.startswith('"') and name.endswith('"'): TypeError: startswith first arg must be bytes or a tuple of bytes, not str....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found