[英]How can i connect two computers with python socket?
im new here.我是新来的。 I have a problem with connection between two computers connected with different wi-fi's.
我在使用不同 wi-fi 连接的两台计算机之间连接时遇到问题。 After about 20 seconds i get information that connection can't be done: There is my code: SERVER:
大约 20 秒后,我得到无法完成连接的信息:有我的代码:服务器:
from socket import *
lista = ['computer']
s = socket(AF_INET, SOCK_STREAM)
port = 21312
s.bind(('my ipv4', port))
s.listen(5)
while True:
for i in range (0, len(lista)):
a = str(lista[i]).encode()
c, addr = s.accept()
print("CONNECTION WITH",addr)
c.send(a)
print(a)
c.close()
CLIENT:客户:
import socket
from socket import *
port = 21312
while True:
s = socket(AF_INET,SOCK_STREAM)
s.connect(('my ipv4', port))
odebrana = (s.recv(1024))
decoded = odebrana.decode()
print(decoded)
s.close()
So I faced the similar problem while sending image files between 2 computers using python sockets .所以我在使用python sockets在两台计算机之间发送图像文件时遇到了类似的问题。 I solved the issue by following this way:
我通过以下方式解决了这个问题:
Note : server.py should be in one computer and client.py should be in another computer.注意: server.py应该在一台计算机上, client.py应该在另一台计算机上。
server.py服务器.py
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
print(host)
server.bind((host, 12000))
server.listen()
client_socket, client_address = server.accept()
file = open('server_image.jpg','wb')
image_chunk = client_socket.recv(2048)
while image_chunk:
file.write(image_chunk)
image_chunk = client_socket.recv(2048)
file.close()
client_socket.close()
client.py客户端.py
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # AF_INET = IP, SOCK_STREAM = TCP
server_host = 'LAPTOP-1231' # Replace this hostname with hostname printed in server.py
client.connect((server_host, 12000)) # 127.0.0.1
file = open('That_Sinking_Feeling_27.jpg', 'rb')
image_data = file.read(2048)
while image_data:
client.send(image_data)
image_data = file.read(2048)
file.close()
client.close()
Now you should add the image in the directory where client.py is located, so that you can send it to another computer (server).现在您应该将图像添加到client.py所在的目录中,以便您可以将其发送到另一台计算机(服务器)。 Rename it to img.jpg
将其重命名为img.jpg
Then, you need to run server.py in your another computer.然后,您需要在另一台计算机上运行server.py 。 It will print the
hostname
in terminal.它将在终端中打印
hostname
。 Then copy that hostname
and paste it in client.py (server_host = hostname_from_server)然后复制该
hostname
并将其粘贴到client.py (server_host = hostname_from_server)
Then run client.py然后运行client.py
Finally the image will be transferred to new computer (server)最后将图像传输到新计算机(服务器)
Likely you are experiencing an issue because your server sits behind a Network Address Translator (NAT).您可能会遇到问题,因为您的服务器位于网络地址转换器 (NAT) 后面。 This way your client cannot use the server's IP directly since it is not reachable.
这样,您的客户端无法直接使用服务器的 IP,因为它无法访问。 There are a few ways around it.
有几种方法可以解决它。
Disclaimer : I have not used hamachi in about 15 years, but just went through the process because of one of the comments below.免责声明:我大约 15 年没有使用过 hamachi,但由于以下评论之一而刚刚经历了这个过程。
Seems like you can create an account, then once you turn it on you should see your v4 and v6 addresses as shown below:似乎您可以创建一个帐户,然后一旦打开它,您应该会看到您的 v4 和 v6 地址,如下所示:
Highlighted is my v4 address.突出显示的是我的 v4 地址。 I suspect you need to create a network, join both PCs in the same network and then use hamachi's IP to emulate behaviour as if they were connected via LAN.
我怀疑您需要创建一个网络,将两台 PC 加入同一网络,然后使用 hamachi 的 IP 模拟行为,就好像它们通过 LAN 连接一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.