通过Python3套接字模块将您的IP发送到VPS

时间:2018-01-08 21:11:07

标签: python python-3.x sockets vps

我正在研究从我的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:在有人将此问题标记为重复或提供类似问题的链接之前,我想提一下,我已经经历了多个问题和网站,我根本无法理解。我需要一个在noob-level'。

解释的答案

提前谢谢你:)

----------- ----------- SOLUTION

我找到了一个解决方案,主要归功于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

1 个答案:

答案 0 :(得分:1)

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

  

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

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

的产率:

('202.149.20.224', 51050)