I tried to communicate with a remote server by Socket communication with Python.

ss.png

Purpose

--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.

environment

Server side

Prepare the server. In my example, I borrowed Sakura's VPS.

Environment

(Note) Precautions when using 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.

sakura01.png

In this example, I wanted to use the port number of 50000, so I turned it off temporarily.

image.png

Port settings

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

install php

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

Build a socket server using PHP-WebSocket

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 WebSocket server

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 (Python)

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())

Try to hit

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.

ss.png

[root@myserver]# php server.php 
2020-09-14 15:00:53 [info] Server created
2020-09-14 15:00:55 [info] [client XXX] Connected

in conclusion

It was unexpectedly troublesome. .. Thank you for visiting.

Recommended Posts

I tried to communicate with a remote server by Socket communication with Python.
I made a server with Python socket and ssl and tried to access it from a browser
I tried to draw a route map with Python
I tried to automatically generate a password with Python3
I tried SMTP communication with Python
I tried a functional language with Python
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
[3rd] I tried to make a certain authenticator-like tool with python
[Python] A memo that I tried to get started with asyncio
I tried to create a list of prime numbers with python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
Socket communication with Python
Python: I tried to make a flat / flat_map just right with a generator
I tried to create a program to convert hexadecimal numbers to decimal numbers with python
I want to make a game with Python
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I tried to discriminate a 6-digit number with a number discrimination application made with python
I tried to automate sushi making with python
[Outlook] I tried to automatically create a daily report email with Python
I tried to build a Mac Python development environment with pythonz + direnv
I want to write to a file with Python
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to make a simple mail sending application with tkinter of Python
[Patent analysis] I tried to make a patent map with Python without spending money
When I tried to create a virtual environment with Python, it didn't work
[ES Lab] I tried to develop a WEB application with Python and Flask ②
When I connect to a remote Jupyter Server with VScode, it's remote but local
I tried to easily create a fully automatic attendance system with Selenium + Python
A story that I was addicted to when I made SFTP communication with python
I tried to create a table only with Django
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I tried to implement a pseudo pachislot in Python
I tried to get started with blender python script_Part 02
I tried to implement an artificial perceptron with python
I want to work with a robot in python.
[Python] I tried running a local server using flask
I tried to solve the problem with Python Vol.1
I tried to analyze J League data with Python
I want to run a quantum computer with Python
I tried to solve AOJ's number theory with Python
I tried fp-growth with python
I tried gRPC with Python
I tried scraping with python
I tried to extract various information of remote PC from Python by WMI Library
When I tried to do socket communication with Raspberry Pi, the protocol was different
[Mac] I want to make a simple HTTP server that runs CGI with Python
I tried to automatically generate a character string to be input to Mr. Adjustment with Python
[Python] I tried to automatically create a daily report of YWT with Outlook mail
I tried to aggregate & compare unit price data by language with Real Gachi by Python
I tried scraping food recall information with Python to create a pandas data frame
I tried to implement a volume moving average with Quantx