2017-04-18 156 views
0

我完全是Python和計算機網絡的新手。在Uni項目上工作時,我遇到了一個問題。我究竟做錯了什麼?我非常感謝任何幫助。UDP TypeError:不支持的操作數類型爲+:'NoneType'和'str'

下面是服務器端:

import socket 

def Main(): 
    host = "127.0.0.1" 
    port = 5000 

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
    s.bind((host, port)) 

    print ("Server Started.") 
    while True: 
     data, addr = s.recvfrom(1024) 
     print ("message from: ") + str(addr) 
     print ("from connected user: ") + str(data.decode('utf-8')) 
     data = str(data).upper() 
     print ("sending: ") + str(data) 
     s.sendto(data, addr) 

    s.close() 

if __name__ == '__main__': 
    Main() 

這裏是我的客戶端:

import socket 

def Main(): 
    host = "127.0.0.1" 
    port = 5000 

    server = ('127.0.0.1', 5000) 

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
    s.bind((host, port)) 

    message = input('->') 
    while message != 'q': 
     s.sendto(message.encode('utf-8'), server) 
     data, addr = s.recvfrom(1024) 
     print ('Received from server: ') + str(data) 
     message = input('->') 
    s.close() 

if __name__ == '__main__' : 
    Main() 

回答

1

有一對夫婦的問題;主要是與印刷。

你有幾個例子print('some text') + str(data);這是行不通的,因爲雖然print()輸出到屏幕(標準輸出),它返回None,所以你實際上在做正試圖拼接None + str(data)

你需要的是print('some text' + str(data))

此外,服務器端存在問題,您將從客戶端接收的數據回顯給客戶端 - 需要將其重新編碼爲字節數組(它以bytearray形式進入,轉換爲一個用於顯示的utf-8字符串,它在回覆之前需要返回bytearray)。

總之,服務器:

import socket 


def Main(): 
    host = "127.0.0.1" 
    port = 5000 

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
    s.bind((host, port)) 

    print("Server Started.") 
    while True: 
     try: 
      data, addr = s.recvfrom(1024) 
      print("message from: " + str(addr)) # moved string concatenation inside print method 
      print("from connected user: " + str(data.decode('utf-8'))) # moved string concatenation inside print method 
      data = str(data).upper() 
      print("sending: " + str(data)) # moved string concatenation inside print method 
      s.sendto(data.encode('utf-8'), addr) # needed to re-encode data into bytearray before sending 
     except KeyboardInterrupt: # added for clean CTRL + C exiting 
      print('Quitting') 
      break 

    s.close() 


if __name__ == '__main__': 
    Main() 

而且客戶端:

import socket 


def Main(): 
    host = "127.0.0.1" 
    port = 5001 

    server = ('127.0.0.1', 5000) 

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
    s.bind((host, port)) 

    message = input('->') 
    while message != 'q': 
     try: 
      s.sendto(message.encode('utf-8'), server) 
      data, addr = s.recvfrom(1024) 
      print('Received from server: ' + str(data)) # moved string concatenation inside print method 
      message = input('->') 
     except KeyboardInterrupt: # added for clean CTRL + C exiting 
      print('Quitting') 
      break 

    s.close() 


if __name__ == '__main__': 
    Main() 
+1

謝謝愛德華這麼多。這在客戶端:print('Received from server:'+ str(data.decode('utf-8')))? –

+0

良好的皮卡 - 是的,你需要在客戶端。在收到數據後,根據需要轉換數據可能更容易,那麼每次使用它時都不必使用它。例如:'data = data.decode('utf-8')',那麼你可以使用數據作爲字符串(它會是)。 –

相關問題