简体   繁体   English

Python /套接字:如何将文件发送到其他网络上的另一台计算机?

[英]Python/socket: How to send a file to another computer which is on a different network?

The codes below work if the computers are on the same network. 如果计算机在同一网络上,则下面的代码将起作用。 However if these computers are on different networks, the connection timed out. 但是,如果这些计算机在不同的网络上,则连接超时。

The codes of server.py are: server.py的代码是:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("192.168.1.**", 12345))

s.listen(10)
c, addr = s.accept()
print('{} connected.'.format(addr))

f = open("image.jpg", "rb")
datas = f.read(1024)

while datas:
    c.send(datas)
    datas = f.read(1024)

f.close()
print("Done sending...")

And the client.py includes: 并且client.py包括:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.1.**", 12345))

f = open("recieved.jpg", "wb")

while True:
    datas = s.recv(1024)
    while datas:
        f.write(datas)
        datas = s.recv(1024)
    f.close()
    break
print("Done receiving")

I read that, the problem can rise from modem settings. 我读到,问题可能出自调制解调器设置。 Then i closed the firewall of the network which server.py connected on. 然后我关闭了连接server.py的网络的防火墙。 But still the computer which client.py file is in, can't connect to the other computer. 但是client.py文件所在的计算机仍无法连接到另一台计算机。

What should i do, to connect these computers? 连接这些计算机该怎么办?

Thanks in advance. 提前致谢。

Try this: 尝试这个:

server.py : server.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import socket
import os

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("", 12345)) #if the clients/server are on different network you shall bind to ('', port)

s.listen(10)
c, addr = s.accept()
print('{} connected.'.format(addr))

f = open("image.jpg", "rb")
l = os.path.getsize("image.jpg")
m = f.read(l)
c.send_all(m)
f.close()
print("Done sending...")

client.py : client.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("server_public_ip", 12345)) # here you must past the public external ipaddress of the server machine, not that local address

f = open("recieved.jpg", "wb")
data = None
while True:
    m = s.recv(1024)
    data = m
    if m:
        while m:
            m = s.recv(1024)
            data += m
        else:
            break
f.write(data)
f.close()
print("Done receiving")

note : on your server.py you are waiting for 10 clients but you accept only one connection you shall put the c, addr = s.accept() in a while loop 注意 :在server.py上,您正在等待10个客户端,但是您只接受一个连接,因此应将c, addr = s.accept()放入while循环中

Update : If the clients/server are behind a rooter then you have to forward the port on the rooter for that connection 更新 :如果客户端/服务器在rooter后面,则必须转发rooter上用于该连接的端口

Port Forwarding : 端口转发

Works only WITH python 2.X 仅适用于python 2.X

i've made myself a script to forward port on every OS but the script it too long you can grab it here 我已经为自己编写了一个脚本,可以在每个操作系统上转发端口,但是脚本太长了,您可以在这里获取

then in server.py : 然后在server.py中

from port_forwarding import forward_port

and before s = socket.socket ### put 并且在s = socket.socket ###之前

forward_port(port, 'description')

don't forget to put the port_forwarding script in the same folder of sever.py 不要忘了把port_forwarding脚本sever.py的同一个文件夹

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python-如何使用套接字将文件发送到其他网络 - python- how to send file to different network using socket 如何通过 python 套接字程序将数据从一台计算机发送到同一 WiFi 网络上的另一台设备? - How to send the data from one computer to another device on the same WiFi network via python socket program? 如何使用 python 向另一台计算机发送请求 - How to send requests to another computer with python Python:如何在不同的网络上连接套接字 - Python : How to connect socket on different network 如何使用python socket模块向远程计算机发送消息? - How to send messages to a remote computer using python socket module? 如何检查在小型网络中的另一台计算机上是否正在运行相同的python脚本? - How to check if a the same python script is running in another computer in a small network? 如何用 Python Sockets 连接到同一网络上的另一台计算机 - How to connect with Python Sockets to another computer on the same network 如何使用python网络编程在另一台计算机上运行程序? - how to run a program in another computer using python network programming? 如何从一个套接字接收数据并将其发送到python中的另一个套接字? - How to recieve data from a one socket and send it to another socket in python? 将excel文件读入存在于不同网络位置的python - Reading an excel file into python which is present at different network location
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM