【问题标题】:Send your IP to VPS through Python3 socket module通过 Python3 套接字模块将您的 IP 发送到 VPS
【发布时间】:2018-01-08 21:11:06
【问题描述】:

我正在研究一种从我的 VPS(在 python3 中)控制我的笔记本电脑的方法。到目前为止,我有一些代码(在笔记本电脑上)通过套接字从服务器接受批处理命令,并且(在服务器上)可以通过套接字向指定的 IP 发送命令。我的问题是我的笔记本电脑的 IP 在我连接到不同的网络时或由于 DHCP 一段时间后会发生变化。

我现在正在做的是将我当前的 IP 发送到服务器(具有静态 IP)的一种方式。

问题是 IP 的长度可能不同,因此我无法在服务器程序上指定大小。

到目前为止我的代码如下:

在 VPS 上运行的程序:

from socket import socket, AF_INET, SOCK_DGRAM, gethostbyname
import sys

#This part is for getting the client IP#
getPORT_NUMBER = 8080
getSIZE = 1024
gethostName = gethostbyname( '0.0.0.0' )

getSocket = socket( AF_INET, SOCK_DGRAM )
getSocket.bind( (hostName, getPORT_NUMBER) )

print("Waiting for client connection on port: {0}\n".format(getPORT_NUMBER))
while True:
        (received,addr) = getSocket.recvfrom(getSIZE)
        ipGet = received.decode("utf-8")
sys.exit()
#-------------#
PORT = 80
SIZE = 1024

mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.connect( (SERVER_IP, PORT) )
print ("Ready to send packets to host: {0}, on port {1}".format(SERVER_IP, PORT))
while True:
        sendMsg = input('rootApp@' + SERVER_IP + ':~# ')
        mySocket.sendto(sendMsg.encode('utf-8'),(SERVER_IP,PORT))
sys.exit()

在我的笔记本电脑上运行的代码:

from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM, gethostname
import sys
import os

PORT_NUMBER = 80
SIZE = 1024
C_IP = gethostbyname(gethostname())
hostName = gethostbyname( '0.0.0.0' )
#This part is for sending the IP to the server#
SERVER_IP   = 'real.server.ip.here'
PORT = 8080
SIZE = 1024

server = socket( AF_INET, SOCK_DGRAM )
server.connect( (SERVER_IP, PORT) )
server.sendto(C_IP.encode('utf-8'),(SERVER_IP,PORT))
#---------------#
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )

print("Listening on port {0}\n".format(PORT_NUMBER))
print('--Command History--')
while True:
        (received,addr) = mySocket.recvfrom(SIZE)
        command = received.decode("utf-8")
        print(command)
        os.system(command)
sys.exit()

我知道代码中可能存在各种错误,希望能指出它们,但我主要是在寻找实际问题的答案。

注意 1:如果有人能提供一些我可以使用的简单加密,我将不胜感激。

注意 2: 在有人将此问题标记为重复问题或提供指向类似问题的链接之前,我想提一下,我已经浏览了多个我根本无法理解的问题和网站.我需要一个“菜鸟级别”的答案。

提前谢谢你:)

------------解决方案-----------

我找到了一个解决方案,这主要归功于 georgexsh(和 Google :)。虽然这可能不是最好或最有效的方法,但它确实有效(在 python3 上,在 Win7 上测试)。

代码是:

在笔记本电脑上运行:

from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM, gethostname
import sys
import os

PORT_NUMBER = 80
SIZE = 1024
hostName = gethostbyname( '0.0.0.0' )
#---------------#
print('Connecting to server...')
SERVER_IP = 'real.server.ip.here' # Make sure you edit this
PORT = 8080 # Any port other than the port you set on the other variable
getSIZE = 1
sendConn = 'A'
print('...') # All print() statements are optional
server = socket( AF_INET, SOCK_DGRAM )
server.connect( (SERVER_IP, PORT) )
server.sendto(sendConn.encode('utf-8'),(SERVER_IP, PORT))
print('...')
#---------------#
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.bind( (hostName, PORT_NUMBER) )
print('...')
print('Connected!\n')
print("Listening on port {0}\n".format(PORT_NUMBER))
print('--Command History--')
while True:
        (received,addr) = mySocket.recvfrom(SIZE)
        command = received.decode("utf-8")
        print(command)
        os.system(command) # Execute the command in cmd. This can be replaced with anything you want your program to do with the data you send.
sys.exit() # Optional

在 VPS 上运行的代码:

from socket import socket, AF_INET, SOCK_DGRAM, gethostbyname
import sys

#-------------#
getPORT_NUMBER = 8080 # Port numbers have to match the ones in the code above!
getSIZE = 1
gethostName = gethostbyname( '0.0.0.0' )

getSocket = socket( AF_INET, SOCK_DGRAM )
getSocket.bind( (gethostName, getPORT_NUMBER) )

print("Waiting for connection on port {0}\n".format(getPORT_NUMBER))
dataGet = ''
while dataGet != 'A' :
        dataGet, clientAddr = getSocket.recvfrom(getSIZE)
        dataGet = dataGet.decode('utf-8')
#-------------#
PORT = 80
SIZE = 1024
SERVER_IP = clientAddr[0] # Important, this caused the error I wrote about in the comments under the answer!
mySocket = socket( AF_INET, SOCK_DGRAM )
mySocket.connect( (SERVER_IP, PORT) )
print ("Ready to communicate with host: {0}, on port {1}".format(SERVER_IP, PORT))
while True:
        sendMsg = input('rootApp@' + SERVER_IP + ':~# ') # Input text is editable.
        mySocket.sendto(sendMsg.encode('utf-8'),(SERVER_IP,PORT))
sys.exit() # Optional

【问题讨论】:

  • "我的笔记本电脑" 检查个人资料图片 hmmmm -_-
  • @SuperStew 那张个人资料照片很旧。很久以前就想换了无论如何...这有什么关系?
  • 仅仅因为 Kali 是“黑客 linux”而你在谈论远程控制计算机
  • 是的,我知道。我的笔记本电脑上有它(双启动)。如果它让你烦恼,我可以改变它吗?编辑:只是为了让你不要认为我是脚本小子,我不认为自己是黑客,我只是在学习。
  • 哈哈,伙计,只是在开玩笑。

标签: python python-3.x sockets vps


【解决方案1】:

为什么不直接使用recvfrom()返回的地址呢?

...返回值是一对(字节,地址),其中字节是表示接收到的数据的字节对象,地址是发送数据的套接字的地址。

received, addr = getSocket.recvfrom(getSIZE)
print(addr)

产量:

('202.149.20.224', 51050)

【讨论】:

  • 嗯,对不起,我不太明白你的意思。我是否只需将(received,addr) 替换为received, addr?所以只需摆脱括号。对不起,如果这是一个明显的问题,但我是 python 中套接字的菜鸟。
  • 括号在这里无关...客户端的IP地址在addr var中,试试看。
  • 所以,等等,我可以通过套接字发送任何东西并使用它来获取它来自的 IP?
  • 是的,如果你只想获取IP,那么socket是如何工作的,服务器必须知道客户端的地址才能传回数据。
  • 好的,我明天去测试一下。如果它对我有用,我会立即接受你的回答。谢谢。这是一种超级简单的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-03
  • 1970-01-01
  • 1970-01-01
  • 2014-08-10
  • 2016-04-14
  • 1970-01-01
相关资源
最近更新 更多