Python - 服务器和客户端问题 [英] Python - Server and client problems

查看:95
本文介绍了Python - 服务器和客户端问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个基本的服务器和客户端脚本。想法是客户端可以连接到服务器并执行命令。 Kinda喜欢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.

推荐答案

当你在套接字上使用makefile(),然后在其上使用readlines

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.

你可能还想要一些实际的协议,所以服务器告诉客户端HERE COMES答复和这是答复的结束,以便客户知道。否则,它很难知道什么时候停止等待更多的响应。 :)

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:

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\n" % rc)
        except Exception:
            conn.close()
            sys.exit()

client.py

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 - 服务器和客户端问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆