Not able to read input from client program
I am trying to write Client-server code. I have added a menu in client code. and depending on input from a client I tried to add cases in the server code. I am able to send the client selected choice to server code, but not able to select a case from the received data. Here is my code.
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:UsersrahulDesktop'
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
Try adding except block too in client script, your code will work after that. And I will suggest you to use raw_input for input purposes in sockets, and format the data type at input to avoid any error in program.
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'
This worked fine.
链接地址: http://www.djcxy.com/p/56440.html上一篇: python ssl服务器发送变量到函数
下一篇: 无法读取来自客户端程序的输入