[英]python connect to server on same network
I have got a server.py and a client.py.我有一个 server.py 和一个 client.py。
If i run them on the same computer using the same port and host as 127.0.0.1 it works fine.如果我使用与 127.0.0.1 相同的端口和主机在同一台计算机上运行它们,则它工作正常。
I got another laptop connected to the same network.我将另一台笔记本电脑连接到同一网络。 I got my local ip address 129.94.209.9 and used that for the server.
我得到了我的本地 IP 地址 129.94.209.9 并将其用于服务器。 On the other laptop I tried connecting to the server but I couldn't.
在另一台笔记本电脑上,我尝试连接到服务器,但无法连接。
Is this an issue with my code, with the network or I'm just using the wrong ip address?这是我的代码、网络的问题还是我只是使用了错误的 IP 地址?
Server.py服务器.py
HOST = '129.94.209.9'
PORT = 9999
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
sockfd, addr = server_socket.accept()
send and receive messages etc....
Client.py客户端.py
HOST = '129.94.209.9'
PORT = 9999
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((HOST, PORT))
except:
print("Cannot connect to the server")
send and receive messages etc...
Client prints "Cannot connect to the server"客户端打印“无法连接到服务器”
Thanks!!谢谢!!
UPDATES: thanks for the comments更新:感谢您的评论
1) I did do sockfd, addr = server_socket.accept() sorry I forgot to add it, it was a few lines further down in the code. 1) 我确实做了 sockfd, addr = server_socket.accept() 对不起,我忘了添加它,它在代码的后面几行。
2) The error is: [Errno 11] Resource temporarily unavailable 2)错误为:[Errno 11] 资源暂时不可用
3) Ping does work 3)Ping确实有效
EDIT: It is working now when I plug both computers in by ethernet cable to the same network.编辑:当我通过以太网电缆将两台计算机插入同一个网络时,它现在正在工作。 Not sure why my the won't work when they are right next to each other connected to wifi.
不知道为什么当它们彼此相邻并连接到 wifi 时我的无法工作。 Thanks for all the suggestions!
感谢所有的建议! I'll investigate the network issue myself
我会自己调查网络问题
Using the following code (my IP address rather than yours, of course) I see the expected behaviour.使用以下代码(当然是我的 IP 地址而不是您的 IP 地址),我看到了预期的行为。
so8srv.py: so8srv.py:
import socket
HOST = '192.168.33.64'
PORT = 9999
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen(10)
print("Listening")
s = server_socket.accept()
print("Connected")
so8cli.py: so8cli.py:
import socket
HOST = '192.168.33.64'
PORT = 9999
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.connect((HOST, PORT))
except Exception as e:
print("Cannot connect to the server:", e)
print("Connected")
When I run it, the server prints "Listening"
.当我运行它时,服务器打印
"Listening"
。 When I then run the client, both it and the server print "Connected"
.当我然后运行客户端时,它和服务器都打印
"Connected"
。
You will notice that, unlike your code, my client doesn't just print hte same diagnostic for any exception that's raised, but instead reports the exception.您会注意到,与您的代码不同,我的客户端不只是为引发的任何异常打印相同的诊断信息,而是报告异常。 As a matter of sound practice you should avoid bare
except
clauses, since your program will then take the same action whether the exception is caused by a programming error or a user action such as KeyboardInterrupt
.作为一个合理的做法,您应该避免使用裸露的
except
子句,因为无论异常是由编程错误还是由诸如KeyboardInterrupt
类的用户操作引起,您的程序都会采取相同的操作。
也可以尝试使用这种结构来获取 IP 地址:
HOST=socket.gethostbyname(socket.gethostname())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.