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

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

im having problems in setting ip address in python socket library

问题

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

服务器端:

import socket
import os
os.system('clear')
s = socket.socket()
ip = 'localhost'
port = 9999
s.bind((ip, port))
s.listen()
print('请等待...')
c, addr = s.accept()
print('有人加入了!')

class color:
    GREEN = '3[92m'
    RED = '3[91m'
    WHITE = '3[0m'

while True:
    msg = input(color.RED + '你的消息:')
    c.send(msg.encode('utf8'))
    print(color.GREEN + c.recv(1024).decode())

客户端:

import socket
import os
os.system('clear')
s = socket.socket()
ip = '192.168.1.3'
port = 9999
s.connect((ip, port))
print('你已连接')
class color:
    GREEN = '3[92m'
    RED = '3[91m'
    WHITE = '3[0m'

while True:
    print(color.RED + s.recv(1024).decode())
    msg = input(color.GREEN + '你的消息:')
    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:

import socket
import os
os.system('clear')
s = socket.socket()
ip = 'localhost'
port = 9999
s.bind((ip,port))
s.listen()
print ('please wait...')
c , addr =s.accept()
print ('someone has joined!')

class color : 
    GREEN = '3[92m'
    RED = '3[91m'
    WHITE = '3[0m'

while True :
    msg = input(color.RED + 'your message: ' )
    c.send(msg.encode('utf8'))
    print (color.GREEN + c.recv(1024).decode())

client side :

import socket
import os
os.system('clear')
s=socket.socket()
ip='192.168.1.3'
port=9999
s.connect((ip,port))
print('you have been connected' )
class color :
    GREEN = '3[92m'
    RED = '3[91m'
    WHITE = '3[0m'

while True:
    print (color.RED + s.recv(1024).decode())
    msg = input(color.GREEN + 'your message : ')
    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 :

connectionrefusederror: [WinError 10061] no connection could be made 
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:

确定