--Exchange data with a remote VPS server in Python
There are many insecure parts due to the nature of what we are doing, so it is for testing and hobbies only.
Prepare the server. In my example, I borrowed Sakura's VPS.
Add packet filtering settings from the control panel. You can turn it off, but it is not recommended as it is not good for security.
In this example, I wanted to use the port number of 50000
, so I turned it off temporarily.
Set as necessary. This example is Sakura's VPS setting example. Also, change the port number to your liking.
$ firewall-cmd --zone=public --add-port=50000/tcp --permanent
Don't forget to reflect the settings!
$ firewall-cmd --reload
In this example, php7.3 is installed.
remi
$ yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
There are a lot of extra things in it, but please change them as needed.
$ yum -y install --enablerepo=remi,remi-php73 php php-mbstring php-xml php-xmlrpc php-gd php-pdo php-pecl-mcrypt php-mysqlnd php-pecl-mysql
In this example, the external library PHP-WebSocket is used.
Drop Binaries here into your Apache directory.
$ cd /var/www/html
$ wget https://github.com/nicokaiser/php-websocket/archive/php-websocket.zip
$ unzip php-websocket.zip
$ sudo rm -rf php-websocket.zip
After downloading, edit server.php
in php-websocket / server
.
If vim
is not installed, use vi
or install it with yum -y install vim
.
$ cd php-websocket/server
$ vim server.php
Change and save '<global IPv4 address>'
and 50000 (port)
as appropriate.
<?php
error_reporting(0);
require(__DIR__ . '/lib/SplClassLoader.php');
$classLoader = new SplClassLoader('WebSocket', __DIR__ . '/lib');
$classLoader->register();
$server = new \WebSocket\Server('<Global IPv4 address>', 50000);
$server->registerApplication('echo', \WebSocket\Application\EchoApplication::getInstance());
$server->registerApplication('time', \WebSocket\Application\TimeApplication::getInstance());
$server->run();
Then rewrite the handshake function in php-websocket / lib / WebSocket / Connection.php
.
Rewrite the handshake
function on the 28
line as follows.
private function handshake($data)
{
socket_write($this->socket, $data, strlen($data));
$this->application->onConnect($this);
return true;
}
Start with the following command.
$ php server.php
When this happens, the server startup is complete. Move on to the client.
[root@~]# php server.php
2020-09-14 14:02:13 [info] Server created
Client-side code that sends data.
--Replace '<global IPv4 address>'
-- 50000
is the port
import socket
if __name__ == '__main__':
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect(('<Global IPv4 address>', 50000))
data = 'Hello From Python Client!'
sock.send(data.encode())
print(sock.recv(1024).decode())
If the transmitted data is echoed, it is successful. It's a bit of a push, but I was able to do it for the time being.
[root@myserver]# php server.php
2020-09-14 15:00:53 [info] Server created
2020-09-14 15:00:55 [info] [client XXX] Connected
It was unexpectedly troublesome. .. Thank you for visiting.
Recommended Posts