2014-02-19 35 views
0

我的某個大學項目出現問題。我們目前正在做套接字和UDP。無論如何,所以我們不得不做一個非常簡單的程序,服務器,客戶端,密碼名稱驗證。 他希望我們做一些事情的日誌,並創建了一個模塊,其中包含寫入日誌文件的方法,這很好。我從不同的地方叫它,它總是有效。唯一不起作用的時候是從服務器調用的。爲UDP連接創建日誌

import datetime 

###Appends the message to the server log with the current date and time 
def AddToLogServer(message): 
    f = open('Log_Server', 'a') 

    time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 
    f.write(time +" " + message +"\n") 
    f.close() 

###Appends the message to the client log with the current date and time 
def AddToLogClient(message): 
    f = open('Log_Client', 'a') 

    time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 
    f.write(time +" " + message +"\n") 
    f.close() 

這是日誌創作。工作正常。

import socket 
import sys 
import Passwords 
import getpass 
import Log 
###Create a connectionless network socket. 
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
###Maximum size data. 
MAX = 65535 
PORT = 1060 

if sys.argv[1:] == ['server']: 
    ###Set server ip 
    ip = '127.0.0.1' 
    try: 
     s.bind((ip, PORT)) 
    except: 
     ###If the server fails to start this error message and the server should end. 
     print "There is an error, perhaps the ip is already being used." 
     s.close() 
     sys.exit(1) 
    ###Add to the server log that the server has started. 
    Log.AddToLogServer("The server is up with the ip " + ip) 
    print 'Server is up and listening at', s.getsockname() 
    ###Listen for a client to send data to the server. 
    while True: 
     data, address = s.recvfrom(MAX) 
     Passwords.getPasswordsAndUsers() 
     ###Compare the name inputted to the ones in the user list 
     i = Passwords.findUser(data) 
     ###Update client log 
     Log.AddToLogServer(address[0] + " Inputted the name " + data) 
     s.sendto(str(i), address) 
     data, address = s.recvfrom(MAX) 
     t = Passwords.checkPassword(data,i) 
     ###if the password matched the user print out the correct message and send a message to the client 
     if t == 1: 
      Log.AddToLogServer(address[0] + " Inputted the correct password for that user") 
      print address[0] + " Has successfully entered the correct User and Password" 
      s.sendto("The name and password were correct", address) 
     ###if the password did not match the user print out the correct message and send a message to the client 
     else: 
      Log.AddToLogServer(address[0] + " Inputted an incorrect password for that user") 
      print address[0] + " Has failed to provide the correct Password for the inputted User" 
      s.sendto("The password did not match the name", address) 


elif sys.argv[1:] == ['client']: 
    ###Takes in the ip and name as inputs. 
    serverIp = raw_input("Please enter the server ip : "); 
    username = raw_input("Enter your first name please: "); 
    ### Attempts to send to the server with the given ip 
    try: 
     s.sendto(username, (serverIp, PORT)) 
    except: 
     ###If the send fails then an error is shown and it finishes the execution. 
     print "There was a problem sending to the server" 
     s.close() 
     sys.exit(1) 
    ###Attempt to relieve data from the server, if the client does not then write the appropriate message. 
    try: 
     data, address = s.recvfrom(MAX) 
    except: 
     print "There was a problem receiving from the server" 

     s.close() 
     sys.exit(1) 
    data = int(data) 
    ###If the data was -1, then the user did not exist and an error should be displayed. Otherwise ask for the 
    ###Password and send it to the server. 
    if data != -1: 
     password = getpass.getpass("Enter your password please: "); 
     try: 
      s.sendto(password, ('127.0.0.1', PORT)) 
     except: 
      print "There was a problem sending to the server" 
      s.close() 
      sys.exit(1) 
    else: 
     print "This first name was not recognised." 
     sys.exit(1) 
    ###Again try to relieve data from the server and print out the output. 
    try: 
     data, address = s.recvfrom(MAX) 
     print data 
     s.close() 

    except: 
     print "There was a problem receiving to the server" 
     s.close() 
     sys.exit(1) 

客戶端服務器代碼,當服務器在啓動時從服務器調用時,日誌不起作用。

+0

您是否在同一臺計算機上同時運行客戶端和服務器,使用相同的端口? – thebjorn

+0

是的。該程序工作正常,只有日誌記錄不起作用。 – user1817988

+0

在s.sendto(username,(serverIp,PORT))之前打印出serverIp和PORT ... – thebjorn

回答

0

我試過重現問題,但服務器的腳本沒有執行。在我的機器上sys.argv[1:]返回[],所以我修改了這部分的腳本if sys.argv[1:] == ['server']:if sys.argv[1:] == []: 請看我收到的回覆。你應該看看那部分。 if sys.argv[1:] == ['server']:

The server is up with the ip 127.0.0.1 
Server is up and listening at ('127.0.0.1', 1060) 
+0

它適用於我。我用「python file.py server」命令提示符,並且它能正常工作。我遇到的唯一問題是日誌。不是服務器打印。 – user1817988