TypeError:无法将'builtin_function_or_method'对象转换为str隐式SOCKETS

时间:2015-12-05 19:01:10

标签: python

我最近一直在学习网络。所以我尝试做一个我在网上找到的实验。这是一个简单的服务器客户端连接。但是当我尝试运行server.py时,我遇到了这个错误:`

TypeError: Can't convert 'builtin_function_or_method' object to str implicitly

这是server.py:

的代码
import socket # socket module

s = socket.socket() # creates socket object "s"
host = socket.gethostname # gets name of local pc
port = 5567 # Reserve a port for the service (numbers are random)
s.bind((host, port)) # connects our host and port number to our socket

s.listen(5) # waits for client connection (5 maximum connections)
while True: # repeats forever
    c, addr = s.accept # Establishes connectd from anyone who tries to connect (c is client object)
    print('Got connection from, '+ addr ) # prints on server gui its connection
    c.send('Thank you for connecting!') # sends client message
    c.close() # stops all connection to current client

错误发生在第6行。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您忘记调用您的功能:

host = socket.gethostname # gets name of local pc

c, addr = s.accept # Establishes connectd from anyone who tries to connect (c is client object)

socket.gethostnames.accept都是callables,添加()

host = socket.gethostname()

c, addr = s.accept()

您看到的异常是由传入socket.gethostname函数而不是函数产生的函数引起的。