简体   繁体   English

如何用 python 插座连接两台电脑?

[英]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:我通过以下方式解决了这个问题:

  • First I completed writing the connection code of both server.py and client.py首先我完成了server.pyclient.py的连接代码的编写

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.有几种方法可以解决它。

  1. The easiest and not very practical one is: get both machines in the same network, then it should work.最简单但不是很实用的方法是:让两台机器在同一个网络中,然后它应该可以工作。
  2. Get a public IP address for the server.获取服务器的公共 IP 地址。 You can do that by hosting it on a cloud server that provides you with a public IP, eg, aws, azure, google cloud etc.您可以通过将其托管在为您提供公共 IP(例如 aws、azure、谷歌云等)的云服务器上来做到这一点。
  3. In the old days we used hamachi to get a VPN that would connect both machines.在过去,我们使用hamachi来获得可以连接两台机器的 VPN。 Then they can identify each other over that VPN.然后他们可以通过该 VPN 相互识别。 Simply turn on hamachi (or any other VPN solution), run your server, then from your client (connected to the VPN), use the VPN's server IP (hamachi will provide you with one when you setup a network).只需打开 hamachi(或任何其他 VPN 解决方案),运行您的服务器,然后从您的客户端(连接到 VPN),使用 VPN 的服务器 IP(hamachi 将在您设置网络时为您提供一个)。

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 地址,如下所示:

滨町IP

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM