[英]How to fix windows decline error for lan file transfer
我是14歲的軟件設計初學者,但是對python基本知識和網絡知識非常了解。 我最近有一個全新的raspberry pi 3 b +,正在嘗試制作一個python程序,該程序將允許我將hp PC的信息輸入傳輸到pi,以便它可以顯示信息,該項目是為了幫助我上學,我輸入了代碼並運行,但是當我嘗試運行“客戶端代碼”時,這是我的pi可以接收輸入數據的錯誤,我收到一條錯誤消息,說設備拒絕了我的連接請求,我該怎么做才能解決問題? 如果您想查看代碼,我可以發布它,但是我不確定是否有必要。
我只是嘗試在兩個程序中更改端口號,因為這不是問題,並且我是LAN和網絡的新手,所以我沒有嘗試過其他任何方法。
根據要求,我的代碼是:(不是HTML,CSS或HTML。使用該界面更容易。
# send.py import socket # Import socket module port = 60000 # Reserve a port for your service. s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name s.bind((host, port)) # Bind to the port s.listen(5) # Now wait for client connection. print 'Server listening....' while True: conn, addr = s.accept() # Establish connection with client. print 'Got connection from', addr data = conn.recv(1024) print('Server received', repr(data)) filename='mytext.txt' f = open(filename,'rb') l = f.read(1024) while (l): conn.send(l) print('Sent ',repr(l)) l = f.read(1024) f.close() print('Done sending') conn.send('Thank you for connecting') conn.close() # recieve.py import socket # Import socket module s = socket.socket() # Create a socket object host = socket.gethostname() # Get local machine name port = 60000 # Reserve a port for your service. s.connect((host, port)) s.send("Hello server!") with open('received_file', 'wb') as f: print 'file opened' while True: print('receiving data...') data = s.recv(1024) print('data=%s', (data)) if not data: break # write data to a file f.write(data) f.close() print('Successfully get the file') s.close() print('connection closed')
不幸的是,這個答案需要Python 3.5+。
在運行此代碼之前,請確保已確定要用於服務器和客戶端的IP(底部的幫助)。
服務器代碼:
import socket
# FIND IP
# The IP to use will be different depending
# on whether you have WiFi or Ethernet, so
# you need to choose whichever one that is
ips = socket.gethostbyname_ex(socket.gethostname())[-1]
print(*[f"{i}: {j}" for i, j in enumerate(ips)], sep="\n")
ip = ips[int(input(" > "))]
# SELECT PORT
port = 10000
#SETUP SERVER
# Create server socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((ip, port))
# A queue of 1 clients is more than enough for our application
sock.listen(1)
print(f"Listening on {ip}:{port}")
while True:
try:
(clientsock, address) = sock.accept()
# so if there's nothing to read we don't wait too long
clientsock.settimeout(0.01)
ping_string = clientsock.recv(5).decode()
if ping_string == "ping!":
print("ping!")
clientsock.sendall(b"ping!")
else:
print("no ping!")
print(ping_string)
clientsock.sendall(b"nopng")
clientsock.shutdown(1)
clientsock.close()
except KeyboardInterrupt:
# Add a way to safely exit the infinite loop
break
sock.close()
客戶代碼:
import socket
# GET IP
print("IP of server")
ip = input(" > ")
# SELECT PORT
port = 10000
# SETUP SOCKET
# Create server socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
print(f"Conencted to {ip}:{port}")
# so if there's nothing to read we don't wait too long
sock.settimeout(0.01)
sock.sendall(b"ping!")
ping_string = sock.recv(5).decode()
if ping_string == "ping!":
print("ping!")
else:
print("no ping!")
sock.close()
輸出:
服務器:
0: 192.168.56.1
1: 192.168.1.151
> 1
Listening on 192.168.1.151:10000
客戶:
Type ip of server
> 192.168.1.151
Conencted to 192.168.1.151:10000
ping!
如果這不起作用,請確保您的計算機可以ping通您的樹莓派,反之亦然。 進入CMD(假定您的HP PC是Windows),然后鍵入ping ____
(用樹莓派的內部ip地址替換____)。
如果終端似乎無法ping通某些內容,則需要轉到計算機和樹莓派上查找它們是內部 ip,您可以找到如何在線進行操作。
現在,我們將設置您的文件服務器。
服務器代碼:
import socket
# OPEN FILE TO SEND ACROSS
with open("filesend.txt", mode="rb") as file:
file_string = file.read()
# FIND IP
# The IP to use will be different depending
# on whether you have WiFi or Ethernet, so
# you need to choose whichever one that is
ips = socket.gethostbyname_ex(socket.gethostname())[-1]
print(*[f"{i}: {j}" for i, j in enumerate(ips)], sep="\n")
ip = ips[int(input(" > "))]
# SELECT PORT
port = 10000
#SETUP SERVER
# Create server socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((ip, port))
# A queue of 1 clients is more than enough for our application
sock.listen(1)
print(f"Listening on {ip}:{port}")
while True:
try:
(clientsock, address) = sock.accept()
# so if there's nothing to read we don't wait too long
clientsock.settimeout(0.01)
# send length
clientsock.sendall((str(len(file_string)) + ".").encode())
clientsock.sendall(file_string)
print("Sent file!")
response_code = clientsock.recv(1).decode()
if response_code != "0":
print("ERROR! response was not 0")
print(response_code)
clientsock.shutdown(1)
clientsock.close()
except KeyboardInterrupt:
# Add a way to safely exit the infinite loop
break
sock.close()
客戶代碼:
import socket
# SELECT IP
print("IP of server")
ip = input(" > ")
# SELECT PORT
port = 10000
# SETUP SOCKET
# Create server socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
print(f"Conencted to {ip}:{port}")
# so if there's nothing to read we don't wait too long
sock.settimeout(0.01)
# Get length of file sent across
len_string = ""
c = ""
while c != ".":
c = sock.recv(1).decode()
len_string += c
length = int(len_string[:-1])
file_string = sock.recv(length)
# send a status code back to the server
sock.sendall(b"0")
with open("filerecv.txt", mode="wb") as file:
file.write(file_string)
print(file_string.decode())
sock.close()
輸出:
服務器:
0: 192.168.56.1
1: 192.168.1.151
> 1
Listening on 192.168.1.151:10000
客戶:
IP of server
> 192.168.1.151
Conencted to 192.168.1.151:10000
THIS IS A TEST!
再次確保您告訴客戶端連接的IP地址與您從服務器腳本提供的列表中選擇的IP地址相同 。 另外,還要確保可以連接IP地址,即如果PC當前在以太網上,則不要在PC處於WiFi時使用該IP地址。
希望這對您有用。 任何問題都留在評論中:)
視窗
ipconfig
示例輸出:
> ipconfig
Windows IP Configuration
Ethernet adapter Ethernet:
Connection-specific DNS Suffix . : home
IPv6 Address. . . . . . . . . . . : 2a00:23c6:6586:2400:e47a:c60e:812b:1123
IPv6 Address. . . . . . . . . . . : fdaa:bbcc:ddee:0:e47a:c60e:812b:1123
Temporary IPv6 Address. . . . . . : 2a00:23c6:6586:2400:d1fe:95f5:27c3:c4b8
Temporary IPv6 Address. . . . . . : fdaa:bbcc:ddee:0:d1fe:95f5:27c3:c4b8
Link-local IPv6 Address . . . . . : fe80::e47a:c60e:812b:1123%19
IPv4 Address. . . . . . . . . . . : 192.168.1.151
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : fe80::8aa6:c6ff:fe23:7a15%19
192.168.1.254
您正在尋找以下行:
IPv4 Address. . . . . . . . . . . : 192.168.1.151
IP是末尾的任何值(例如192.168.1.151
)
查找您的樹莓派IP
除非您更改了一些高級設置,否則Raspberry Pi的主機名將是raspberrypi
或raspberrypi.local
。 我都看過 如果您想知道IP地址,請使用下面的Python腳本,並嘗試從打印的列表中ping所有IP,以找出Pi實際使用的IP。
在您的PC上運行:
import socket
host1 = "raspberrypi"
host2 = "raspberrypi.local"
try:
ip1 = socket.gethostbyname_ex(host1)[-1]
except:
ip1 = []
try:
ip2 = socket.gethostbyname_ex(host2)[-1]
except:
ip2 = []
print(list(set(ip1+ip2)))
還有其他問題,請讓我知道:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.