模块 'socket' 没有属性 'getsockname'
module 'socket' has no attribute 'getsockname'
这是一个使用 TCP 套接字的简单文件服务器文件,
当我 运行 时,出现以下错误。
谁能告诉我这个问题的解决方案
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = socket.getsockname() # Reserve a port for your service.
block_size = 1024 #file is divided into 1kb size
s.bind((host, port)) # Bind to the port
#f = open('paris.png','wb')
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print('Got connection from', addr)
print("Receiving...")
l = c.recv(block_size)
while (l):
print("Receiving...")
l = c.recv(block_size)
#f.close()
print("Done Receiving")
mssg = 'Thank You For Connecting'
c.send(mssg.encode())
c.close() # Close the connection
Traceback (most recent call last):
File "C:\Users\Hp\Documents\Python codes\file_transfer_simple_server.py",line 5, in <module>
port = socket.getsockname() # Reserve a port for your service.
AttributeError: module 'socket' has no attribute 'getsockname'
正如@Mark Tolonen 在评论中提到的,您在套接字模块上调用了 getsockname()
。 getsockname
是套接字实例上的一个方法 s
import socket
s = socket.socket()
s.bind(('localhost',12345))
host, port = s.getsockname() # unpack tuple
block_size = 1024
print(port)
# output
12345
这是一个使用 TCP 套接字的简单文件服务器文件, 当我 运行 时,出现以下错误。 谁能告诉我这个问题的解决方案
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = socket.getsockname() # Reserve a port for your service.
block_size = 1024 #file is divided into 1kb size
s.bind((host, port)) # Bind to the port
#f = open('paris.png','wb')
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print('Got connection from', addr)
print("Receiving...")
l = c.recv(block_size)
while (l):
print("Receiving...")
l = c.recv(block_size)
#f.close()
print("Done Receiving")
mssg = 'Thank You For Connecting'
c.send(mssg.encode())
c.close() # Close the connection
Traceback (most recent call last):
File "C:\Users\Hp\Documents\Python codes\file_transfer_simple_server.py",line 5, in <module>
port = socket.getsockname() # Reserve a port for your service.
AttributeError: module 'socket' has no attribute 'getsockname'
正如@Mark Tolonen 在评论中提到的,您在套接字模块上调用了 getsockname()
。 getsockname
是套接字实例上的一个方法 s
import socket
s = socket.socket()
s.bind(('localhost',12345))
host, port = s.getsockname() # unpack tuple
block_size = 1024
print(port)
# output
12345