我在Python套接字库中设置IP地址时遇到问题。

huangapple go评论90阅读模式
英文:

im having problems in setting ip address in python socket library

问题

以下是你提供的代码的中文翻译部分:

服务器端:

  1. import socket
  2. import os
  3. os.system('clear')
  4. s = socket.socket()
  5. ip = 'localhost'
  6. port = 9999
  7. s.bind((ip, port))
  8. s.listen()
  9. print('请等待...')
  10. c, addr = s.accept()
  11. print('有人加入了!')
  12. class color:
  13. GREEN = '3[92m'
  14. RED = '3[91m'
  15. WHITE = '3[0m'
  16. while True:
  17. msg = input(color.RED + '你的消息:')
  18. c.send(msg.encode('utf8'))
  19. print(color.GREEN + c.recv(1024).decode())

客户端:

  1. import socket
  2. import os
  3. os.system('clear')
  4. s = socket.socket()
  5. ip = '192.168.1.3'
  6. port = 9999
  7. s.connect((ip, port))
  8. print('你已连接')
  9. class color:
  10. GREEN = '3[92m'
  11. RED = '3[91m'
  12. WHITE = '3[0m'
  13. while True:
  14. print(color.RED + s.recv(1024).decode())
  15. msg = input(color.GREEN + '你的消息:')
  16. s.send(msg.encode('utf8'))

在这里,你需要为每一侧设置一个IP地址。你尝试了多种方法,但都没有成功。你是否有建议或推荐如何设置IP地址?谢谢。

每种方法的错误:

方法1:将服务器端的IP设置为'localhost'或其他gethostname或gethostbyname:
服务器端可以正常工作,但客户端无法连接。

方法2:将服务器IP地址设置为类似'192.168.0.3'的内容:
服务器端出现错误:OSError: [WinError 10049] 请求的地址在其上下文中无效。

主要修改:
你们建议的各种方法在客户端上都会出现错误:

ConnectionRefusedError: [WinError 10061] 无法建立连接,因为目标计算机拒绝了连接

英文:

so i want to make a Messenger and i create it with the socket library. by creating a server side and a client side. where two need to be on same internet ( i think so ! )
here is my code:

server side:

  1. import socket
  2. import os
  3. os.system('clear')
  4. s = socket.socket()
  5. ip = 'localhost'
  6. port = 9999
  7. s.bind((ip,port))
  8. s.listen()
  9. print ('please wait...')
  10. c , addr =s.accept()
  11. print ('someone has joined!')
  12. class color :
  13. GREEN = '3[92m'
  14. RED = '3[91m'
  15. WHITE = '3[0m'
  16. while True :
  17. msg = input(color.RED + 'your message: ' )
  18. c.send(msg.encode('utf8'))
  19. print (color.GREEN + c.recv(1024).decode())

client side :

  1. import socket
  2. import os
  3. os.system('clear')
  4. s=socket.socket()
  5. ip='192.168.1.3'
  6. port=9999
  7. s.connect((ip,port))
  8. print('you have been connected' )
  9. class color :
  10. GREEN = '3[92m'
  11. RED = '3[91m'
  12. WHITE = '3[0m'
  13. while True:
  14. print (color.RED + s.recv(1024).decode())
  15. msg = input(color.GREEN + 'your message : ')
  16. s.send(msg.encode('utf8'))

here , i need to set an ip address for each side. i tried all methods but nothing worked.
do you have a recommendation to how to do it ? thanks.

errors on each method :

method 1 : setting server side ip 'local_host' or any other gethostname or gethostbyname :
server side works fine but client side does not connect

method 2 : setting server ip address to something like '192.168.0.3' :
server side makes an error : OSError : [WinError 10049] The requested address is not valid in its context

edit on main :
the methods you guys recommended give an error on client side :

  1. connectionrefusederror: [WinError 10061] no connection could be made
  2. because the target machine refused it

答案1

得分: 1

不确定是否正确,但根据大多数示例,人们似乎使用 ip = socket.gethostbyname(socket.gethostname()),这与我对C中套接字的了解相符。因此,尝试将
ip = “localhost” 替换为
ip = socket.gethostbyname(socket.gethostname())

正如总统建议的那样,在调试期间,您还可以绑定到IP地址“0.0.0.0”,这将侦听所有接口,尽管这可能不会成为问题。

英文:

I don’t know if this is correct, but it seems from most of the examples that people use ip = socket.gethostbyname(socket.gethostname()), which lines up with what I know about sockets in C. So try replacing
ip = “localhost” with
ip = socket.gethostbyname(socket.gethostname()).

As the President recommended, you could also bind to the ip “0.0.0.0” during debugging as it will listen on all interfaces, although this will probably not be an issue.

huangapple
  • 本文由 发表于 2023年2月9日 00:57:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定