【问题标题】:Python How to build a *real* ip address multi-client serverPython如何构建一个*真实的IP地址多客户端服务器
【发布时间】:2023-03-30 01:40:02
【问题描述】:

假设我在 python 中有一个多客户端服务器套接字和一个客户端套接字。

服务器:(您不必阅读所有服务器的代码,只需知道它是一个多客户端服务器。

import socket, select

CONNECTION_LIST = []    # list of socket clients
RECV_BUFFER = 4096 # Advisable to keep it as an exponent of 2
PORT = 5000

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# this has no effect, why ?
RealServerIP = ? # I want to have a real server ip which would let me connect to the server from any computer around the globe...
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((RealServerIP, PORT))
server_socket.listen(10)

# Add server socket to the list of readable connections
CONNECTION_LIST.append(server_socket)

print "Chat server started on port " + str(PORT)

while 1:
    # Get the list sockets which are ready to be read through select
    read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])

    for sock in read_sockets:

        #New connection
        if sock == server_socket:
            # Handle the case in which there is a new connection recieved through server_socket
            sockfd, addr = server_socket.accept()
            CONNECTION_LIST.append(sockfd)
            print "Client (%s, %s) connected" % addr

        #Some incoming message from a client
        else:
            # Data recieved from client, process it
            try:
                #In Windows, sometimes when a TCP program closes abruptly,
                # a "Connection reset by peer" exception will be thrown
                data = sock.recv(RECV_BUFFER)
                # echo back the client message
                if data:
                    sock.send(data)

            # client disconnected, so remove from socket list
            except:
                broadcast_data(sock, "Client (%s, %s) is offline" % addr)
                print "Client (%s, %s) is offline" % addr
                sock.close()
                CONNECTION_LIST.remove(sock)
                continue

server_socket.close()

(来自http://www.binarytides.com/code-chat-application-server-client-sockets-python/ 的示例)。

还有 3 个客户,它们是您能想象到的最简单的客户:

import socket # imports module named 'socket'
RealServerIP = ? # I need your help here.... read the continuation
my_socket = socket.socket() # creates new socket named 'my_socket'
my_socket.connect((RealServerIP, 5000)) # connects to the server
my_socket.send(str) # sends string to the server
data = my_socket.recv(1024) 
print data # prints data
my_socket.close() 

我想检查我的服务器是否可以同时与这 3 个客户端通信。所以我想让我的服务器成为一个公共服务器,比如 Facebook 的网络服务器等。 因此,世界各地的任何计算机都可以连接到它。

所以,我试图弄清楚如何使用与我的本地主机无关的特定 IP 和 PORT 在线存储我的 python 服务器套接字,我希望它是真实的!就像你知道的任何聊天/网络服务器一样..

【问题讨论】:

  • 你需要绑定你的ip地址而不是环回接口。或在您的路由器/防火墙处转发到服务器绑定到的任何地址/端口。
  • 谢谢。可以举个例子吗?

标签: python sockets ip


【解决方案1】:

如果您的意思是用户通过它连接到互联网的 IP,我认为没有办法通过 socket 模块获取它,但我有办法使用 requests 做同样的事情模块,如果这可能有帮助。

import requests

def get_ip():
    url = 'https://www.cmyip.com'
    user_ip = requests.get(url).content.decode("utf-8").split("My IP Address is ")[1].split(" <a class=")[0]
    return user_ip

希望对你有所帮助。

注意:这是在 python 3.4.3 上测试的,它工作正常。我不知道 2.X 版本。

【讨论】:

    猜你喜欢
    • 2011-07-19
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    相关资源
    最近更新 更多