[英]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 : 端口转发 :
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.