带套接字的python代码中的AttributeError

时间:2019-04-28 23:25:09

标签: python sockets tcp

我有一个AttributeError,上面写着“ AttributeError:'socket'对象没有属性'upper'”。我很确定自己已正确创建并建立了连接,但在咨询套接字文档后仍然不确定该怎么做。

谢谢。


import socket
from _thread import *
import threading

print_lock = threading.Lock()
list_of_clients = [] 

def threaded(data, addr, s):
  s.sendto(data.upper(), addr)
  while True:
    message = s.recv(1024)
    if not message:
      print('Bye')
      print_lock.release()
      break
    message = message.upper()
    print("Sending message to " + addr[0])
    s.sendto(message, addr)
  data.close()

def Main():

  list_of_clients = []
  serverName = 'localhost'
  serverPort = 12000

  with socket.socket(socket.AF_INET , socket.SOCK_STREAM) as serverSocket:
    serverSocket.connect((serverName, serverPort))
    while True :
      print('Ready to ping...')
      data, addr = serverSocket.accept()
      print(type(data))
      print_lock.acquire()
      print("Client connected ip:<" + str(addr) + ">")

      start_new_thread(threaded, (data, addr, serverSocket))
      print("Continue")

if __name__ == '__main__':
  Main()

1 个答案:

答案 0 :(得分:0)

问题是这一行:

List_of_entries[0].get()

s.sendto(data.upper(), addr) 在这里定义:

data

data, addr = serverSocket.accept() 方法返回一个能够发送和接收数据的2元组where the first element is a socket object

因此,您应该传递accept的第一个元素,以及要发送的任何数据,而不是将serverSocket传递给threaded