【问题标题】:Not able to read input from client program无法从客户端程序读取输入
【发布时间】:2017-05-22 16:27:58
【问题描述】:

我正在尝试编写客户端-服务器代码。我在客户端代码中添加了一个菜单。并且根据来自客户端的输入,我尝试在服务器代码中添加案例。我能够将客户端选择的选项发送到服务器代码,但无法从接收到的数据中选择一个案例。这是我的代码。

server.py

import socket

import sys

import os

s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

print (' socket created s1')    

serveraddress = ('localhost' , 2000)

print ('starting server on', serveraddress)

s1.bind(serveraddress)

s1.listen(3)

while True:

    print('waiting for connection')

    connection, clientaddress = s1.accept()

    print ('connecting with', clientaddress)

    command = connection.recv(1000)

    print (command)

    if command == '1':

        print('entered into 1st if')

        try:

            filename = connection.recv(1000)

            with open(filename, 'rb') as filetosend:

                for data in filetosend:

                    connection.sendall(data)

        finally:

            connection.close()

    if command == '2':

        print('entered into 2st if')

        filelist = os.listdir('C:\Rahul')

        connection.sendall(filelist)

    if command == '3':

        print('entered into 3st if')

        s1.close()

        break

Client.py

import sys

import os

import socket

s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

serveraddress = ('localhost' , 2000)

print ('connecting to server on'), serveraddress

s1.connect(serveraddress)

try:

    a = input('Enter you choice: \n 1-Get file \n 2-Get List \n 3-quit \n')

    while True:

        if a == 1:

            s1.send('1')

            b = input('enter file name: ')

            s1.send(b)

            downloadDir = r'C:\Users\rahul\Desktop'

            with open(os.path.join(downloadDir, b), 'wb') as filetowrite:

                while True:

                    data = s1.recv(1000)

                    if not data:

                        break

                    filetowrite.write(data)

                filetowrite.close()

            s1.close()

        elif a == 2:

            s1.send('2')

            #s1.sendall(' send all files list ')

            filelist = s1.recv(1000)

            print (filelist)

        elif a == 3:

            x = False

            print('closing connection')

            s1.close()      

finally:
    s1.close

【问题讨论】:

    标签: python sockets server network-programming client-server


    【解决方案1】:

    尝试在客户端脚本中也添加 except 块,之后您的代码将起作用。 我会建议你在sockets中使用raw_input作为输入,并在输入时格式化数据类型以避免程序中的任何错误。

    import sys, os, socket s1 = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serveraddress = ('localhost' , 2000) print ('connecting to server on'), serveraddress s1.connect(serveraddress) try: a = int(raw_input('Enter you choice: \n 1-Get file \n 2-Get List \n 3-quit \n')) while True: if a == 1: s1.send('1') b = str(raw_input('enter file name: ')) s1.send(b) # downloadDir = '\\root\\' # with open(os.path.join(downloadDir, b), 'wb') as filetowrite: # while True: # data = s1.recv(1000) # if not data: # break # filetowrite.write(data) # filetowrite.close() print "It worked till here." s1.close() elif a == 2: # s1.send('2') # #s1.sendall(' send all files list ') # filelist = s1.recv(1000) # print (filelist) print "It also worked till here." elif a == 3: x = False print "Closing Connection" s1.close() except: print 'It Gave an Error'

    这很好。

    【讨论】:

    • 感谢 Vipin 的帮助。这解决了我的问题。我仍然有一些错误。我会努力的。
    • 很高兴它帮助了^_^
    • 但是在读取从客户端发送的输入以进行案例选择时,服务器代码仍然出现错误。
    • 你到底想用这个脚本做什么?
    • 好吧,我添加了一个菜单,就像在客户端中他提供 1 作为输入之后,他应该提供文件名并下载它。如果客户端给出2,他应该得到服务器目录中可用文件的列表,然后客户端可以提供文件名并下载它。如果提供 3,它应该断开程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多