• <tfoot id='tmHwB'></tfoot>
    <i id='tmHwB'><tr id='tmHwB'><dt id='tmHwB'><q id='tmHwB'><span id='tmHwB'><b id='tmHwB'><form id='tmHwB'><ins id='tmHwB'></ins><ul id='tmHwB'></ul><sub id='tmHwB'></sub></form><legend id='tmHwB'></legend><bdo id='tmHwB'><pre id='tmHwB'><center id='tmHwB'></center></pre></bdo></b><th id='tmHwB'></th></span></q></dt></tr></i><div id='tmHwB'><tfoot id='tmHwB'></tfoot><dl id='tmHwB'><fieldset id='tmHwB'></fieldset></dl></div>

    <legend id='tmHwB'><style id='tmHwB'><dir id='tmHwB'><q id='tmHwB'></q></dir></style></legend>
    • <bdo id='tmHwB'></bdo><ul id='tmHwB'></ul>

        <small id='tmHwB'></small><noframes id='tmHwB'>

      1. Python套接字错误TypeError:需要一个类似字节的对象,而不是带有发送功能的'str'

        Python sockets error TypeError: a bytes-like object is required, not #39;str#39; with send function(Python套接字错误TypeError:需要一个类似字节的对象,而不是带有发送功能的str)

          <small id='PPGXr'></small><noframes id='PPGXr'>

        • <i id='PPGXr'><tr id='PPGXr'><dt id='PPGXr'><q id='PPGXr'><span id='PPGXr'><b id='PPGXr'><form id='PPGXr'><ins id='PPGXr'></ins><ul id='PPGXr'></ul><sub id='PPGXr'></sub></form><legend id='PPGXr'></legend><bdo id='PPGXr'><pre id='PPGXr'><center id='PPGXr'></center></pre></bdo></b><th id='PPGXr'></th></span></q></dt></tr></i><div id='PPGXr'><tfoot id='PPGXr'></tfoot><dl id='PPGXr'><fieldset id='PPGXr'></fieldset></dl></div>

            • <bdo id='PPGXr'></bdo><ul id='PPGXr'></ul>
            • <tfoot id='PPGXr'></tfoot>
                <tbody id='PPGXr'></tbody>

              <legend id='PPGXr'><style id='PPGXr'><dir id='PPGXr'><q id='PPGXr'></q></dir></style></legend>

                  本文介绍了Python套接字错误TypeError:需要一个类似字节的对象,而不是带有发送功能的'str'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个程序,该程序将在本地计算机上打开一个端口并让其他人通过 netcat 连接到它.我当前的代码是.

                  I am trying to create a program that will open a port on the local machine and let others connect into it via netcat. My current code is.

                  s = socket.socket()
                  host = '127.0.0.1'
                  port = 12345
                  s.bind((host, port))
                  
                  s.listen(5)
                  while True:
                      c, addr = s.accept()
                      print('Got connection from', addr)
                      c.send('Thank you for connecting')
                      c.close()
                  

                  我是 Python 和套接字的新手.但是当我运行这段代码时,它将允许我使用以下命令发送 netcat 连接:

                  I am new to Python and sockets. But when I run this code it will allow me to send a netcat connection with the command:

                  nc 127.0.0.1 12345
                  

                  但是在我的 Python 脚本中,我收到了 c.send 的错误:

                  But then on my Python script I get the error for the c.send:

                  TypeError: a bytes-like object is required, not 'str'
                  

                  我基本上只是想打开一个端口,允许 netcat 连接并在那台机器上拥有一个完整的 shell.

                  I am basically just trying to open a port, allow netcat to connect and have a full shell on that machine.

                  推荐答案

                  这个错误的原因是在Python 3中,字符串是Unicode,但是在网络上传输时,数据需要是字节.所以...一些建议:

                  The reason for this error is that in Python 3, strings are Unicode, but when transmitting on the network, the data needs to be bytes instead. So... a couple of suggestions:

                  1. 建议使用 c.sendall() 而不是 c.send() 以防止可能出现的问题,即您可能没有通过一次调用发送整个 msg(请参阅 docs).
                  2. 对于文字,为字节字符串添加 'b':c.sendall(b'Thank you for connection')
                  3. 对于变量,您需要将 Unicode 字符串编码为字节字符串(见下文)
                  1. Suggest using c.sendall() instead of c.send() to prevent possible issues where you may not have sent the entire msg with one call (see docs).
                  2. For literals, add a 'b' for bytes string: c.sendall(b'Thank you for connecting')
                  3. For variables, you need to encode Unicode strings to byte strings (see below)

                  最佳解决方案(应同时使用 2.x 和 3.x):

                  Best solution (should work w/both 2.x & 3.x):

                  output = 'Thank you for connecting'
                  c.sendall(output.encode('utf-8'))
                  

                  结语/背景:这在 Python 2 中不是问题,因为字符串已经是字节字符串——您的 OP 代码可以在该环境中完美运行.Unicode 字符串在 1.6 & 版本中被添加到 Python 中.2.0 但在 3.0 成为默认字符串类型之前退居二线.另请参阅 this similar question 以及 this one.

                  Epilogue/background: this isn't an issue in Python 2 because strings are bytes strings already -- your OP code would work perfectly in that environment. Unicode strings were added to Python in releases 1.6 & 2.0 but took a back seat until 3.0 when they became the default string type. Also see this similar question as well as this one.

                  这篇关于Python套接字错误TypeError:需要一个类似字节的对象,而不是带有发送功能的'str'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)

                    <legend id='xLypn'><style id='xLypn'><dir id='xLypn'><q id='xLypn'></q></dir></style></legend>
                    • <bdo id='xLypn'></bdo><ul id='xLypn'></ul>
                      <tfoot id='xLypn'></tfoot>

                          <i id='xLypn'><tr id='xLypn'><dt id='xLypn'><q id='xLypn'><span id='xLypn'><b id='xLypn'><form id='xLypn'><ins id='xLypn'></ins><ul id='xLypn'></ul><sub id='xLypn'></sub></form><legend id='xLypn'></legend><bdo id='xLypn'><pre id='xLypn'><center id='xLypn'></center></pre></bdo></b><th id='xLypn'></th></span></q></dt></tr></i><div id='xLypn'><tfoot id='xLypn'></tfoot><dl id='xLypn'><fieldset id='xLypn'></fieldset></dl></div>
                        1. <small id='xLypn'></small><noframes id='xLypn'>

                              <tbody id='xLypn'></tbody>