通过 Python3 套接字模块将您的 IP 发送到 VPS
Send your IP to VPS through Python3 socket module
我正在研究一种从 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: 在有人将此问题标记为重复或对类似问题给出 link 之前,我想提一下我已经经历了多次我根本无法理解的问题和网站。我需要在 'noob-level'.
解释的答案
提前谢谢你:)
------------解决方案------------
我主要感谢 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
为什么不直接使用recvfrom()
返回的地址?
... The return value is a pair (bytes, address) where bytes is a bytes object representing the data received and address is the address of the socket sending the data.
received, addr = getSocket.recvfrom(getSIZE)
print(addr)
产量:
('202.149.20.224', 51050)
我正在研究一种从 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: 在有人将此问题标记为重复或对类似问题给出 link 之前,我想提一下我已经经历了多次我根本无法理解的问题和网站。我需要在 'noob-level'.
解释的答案提前谢谢你:)
------------解决方案------------
我主要感谢 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
为什么不直接使用recvfrom()
返回的地址?
... The return value is a pair (bytes, address) where bytes is a bytes object representing the data received and address is the address of the socket sending the data.
received, addr = getSocket.recvfrom(getSIZE)
print(addr)
产量:
('202.149.20.224', 51050)