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

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

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

        Python - 服务器和客户端问题

        时间:2023-10-18

            <small id='2ePtg'></small><noframes id='2ePtg'>

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

                  <tfoot id='2ePtg'></tfoot>

                    <tbody id='2ePtg'></tbody>
                  本文介绍了Python - 服务器和客户端问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试创建一个基本的服务器和客户端脚本.这个想法是客户端可以连接到服务器并执行命令.有点像 SSH,但非常简单.这是我的服务器代码:

                  I'm trying to create a basic server and client script. The idea is that the client can connect to the server and execute commands. Kinda like SSH but very simple. Heres my server code:

                  import sys, os, socket
                  
                  
                  host = ''                
                  port = 50103
                  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                  s.bind((host, port))
                  print("Server started on port: ", port)
                  s.listen(1)
                  while (1):
                      conn, addr = s.accept()
                      print 'New connection from ', addr
                      try:
                          while True:
                              rc = conn.recv(2)
                              pipe = os.popen(rc)
                              rl = pipe.readlines()
                              fl = conn.makefile('w', 0)
                              fl.writelines(rl[:-1])
                              fl.close()
                      except IOError:
                              conn.close()
                  

                  这是我的客户:

                  import sys, socket
                  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                  host = 'localhost'
                  port = input('Port: ')
                  s.connect((host, port))
                  while (1):
                      cmd = raw_input('$ ')
                      s.send(cmd) 
                      file = s.makefile('r', 0)
                      sys.stdout.writelines(file.readlines())
                      file.close()
                  

                  这是我的问题.我启动服务器,然后在同一台机器上运行客户端.我输入端口并连接.然后我得到raw_input,它是'$'.如果我键入像ls"这样的命令,它只会挂在客户端.我必须退出服务器才能让客户端接收 ls 的输出.顺便说一句,我正在运行 Ubuntu Linux.不确定这是否重要.

                  Here is my problem. I start the server and then run the client on the same machine. I enter the port and connect. Then I get the raw_input which is the '$'. If I type a command like 'ls' it just hangs on the client side. I have to exit the server for the client to receive the output of ls. By the way I am running Ubuntu Linux. Not sure if that matters.

                  推荐答案

                  当你在 socket 上 makefile() 然后在上面使用 readlines() 时,它会一直持续到你到达文件末尾,在 socket 的情况下是不是从另一端关闭了.

                  When you makefile() on the socket and then use readlines() on it, it will continue until you reach an end of file, which in the socket case is that it closed from the other end.

                  在这种情况下使用 makefile() 对我来说毫无意义,尤其是因为您创建它并在每个命令之后关闭它.只需在两端使用 send() 和 recv() 即可.

                  Using makefile() in this case makes no sense to me, especially since you create it and close it after each command. Just use send() and recv() on both ends.

                  您可能还希望有某种实际的协议",以便服务器告诉客户端这里有一个响应"和这是响应的结束",以便客户端知道.否则很难知道何时停止等待更多响应.:)

                  You probably also want to have some sort of actual "protocol" so the server tells the client "HERE COMES A RESPONSE" and "THIS IS THE END OF THE RESPONSE" so that the client knows. Otherwise it gets hard to know when to stop waiting for more response. :)

                  更新一个有效的例子:

                  server.py:

                  import sys, os, socket
                  
                  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                  s.bind(('', 50500))
                  print("Server started")
                  s.listen(1)
                  while True:
                      print "Accepting"
                      conn, addr = s.accept()
                      print 'New connection from ', addr
                      while True:
                          try:
                              rc = conn.recv(1024)
                              print "Command", rc
                              if not rc.strip():
                                  continue
                  
                              if rc.strip() == 'END':
                                  print "Close"
                                  conn.send("**END**")
                                  conn.close()
                                  break
                              else:
                                  conn.send("This is the result of command %s
                  " % rc)
                          except Exception:
                              conn.close()
                              sys.exit()
                  

                  client.py

                  import sys, os, socket
                  
                  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                  s.connect(('localhost', 50500))
                  while True:
                      cmd = raw_input('$ ')
                      s.send(cmd) 
                      result = s.recv(1024)
                      print result
                      if result == "**END**":
                          print "Ending"
                          break
                  

                  这篇关于Python - 服务器和客户端问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:urllib3 connectionpool - 连接池已满,丢弃连接 下一篇:Python urllib2.open 连接由对等错误重置

                  相关文章

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

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

                  1. <tfoot id='PUXJQ'></tfoot>

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