• <small id='qKHsM'></small><noframes id='qKHsM'>

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

      <i id='qKHsM'><tr id='qKHsM'><dt id='qKHsM'><q id='qKHsM'><span id='qKHsM'><b id='qKHsM'><form id='qKHsM'><ins id='qKHsM'></ins><ul id='qKHsM'></ul><sub id='qKHsM'></sub></form><legend id='qKHsM'></legend><bdo id='qKHsM'><pre id='qKHsM'><center id='qKHsM'></center></pre></bdo></b><th id='qKHsM'></th></span></q></dt></tr></i><div id='qKHsM'><tfoot id='qKHsM'></tfoot><dl id='qKHsM'><fieldset id='qKHsM'></fieldset></dl></div>
        • <bdo id='qKHsM'></bdo><ul id='qKHsM'></ul>
        <tfoot id='qKHsM'></tfoot>
      1. TypeError:需要一个类似字节的对象,而不是“str"

        时间:2024-04-21

              <tfoot id='jJtn5'></tfoot>

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

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

                  <bdo id='jJtn5'></bdo><ul id='jJtn5'></ul>

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

                  问题描述

                  The following is the code that tries to modify the input supplied by a user by using sockets:

                  from socket import *
                  
                  serverName = '127.0.0.1'
                  serverPort = 12000
                  clientSocket = socket(AF_INET, SOCK_DGRAM)
                  message = input('Input lowercase sentence:')
                  clientSocket.sendto(message,(serverName, serverPort))
                  modifiedMessage, serverAddress = clientSocket.recvfrom(2048)
                  print (modifiedMessage)
                  clientSocket.close()
                  

                  When I execute it and supply input the following error occurs:

                  Input lowercase sentence:fdsgfdf
                  Traceback (most recent call last):
                    File "C:srinath filesNETWORKSUDPclient.py", line 6, in <module>
                      clientSocket.sendto(message,(serverName, serverPort))
                  TypeError: a bytes-like object is required, not 'str'
                  

                  What can I do to solve this?

                  解决方案

                  This code is good for Python 2. But in Python 3, results in bit encoding error. I was trying to make a simple TCP server and encountered the same problem. Encoding solves this. Try this with sendto command.

                  clientSocket.sendto(message.encode(),(serverName, serverPort))
                  

                  Similarly you should use .decode() to receive the data on the UDP server side, if you want to print it exactly as it was sent.

                  这篇关于TypeError:需要一个类似字节的对象,而不是“str"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Python,TypeError:不可散列的类型:'list' 下一篇:TypeError:在字符串格式化python期间并非所有参数都转换了

                  相关文章

                    <tfoot id='WVDCJ'></tfoot>
                  1. <small id='WVDCJ'></small><noframes id='WVDCJ'>

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

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