備忘録

備忘録

Python3でMariaDB(MySQL)に接続する方法

Ⅰ. はじめに

タイトルの通り「Python3でMySQLに接続する方法」です。

Ⅱ. やり方

1. 必要なものをインストールする

RedHat系の場合

yum install python3-devel mysql-devel pkgconfig

Debian系 の場合

apt-get install python3-dev default-libmysqlclient-dev build-essential pkg-config
2. mysqlclientをインストールする
pip3 install mysqlclient
3. サンプルプログラムを書く
import MySQLdb

def Select():
    conn = MySQLdb.connect(user = 'user001', password = '12345', host = '127.0.0.1', db = 'my_db', charset = 'utf8')
    cursor = conn.cursor()

    sql = "select 123 from dual"
    cursor.execute(sql)
    results= cursor.fetchall()
    for x in results:
        print(x[0])

    cursor.close()
    conn.close()

def Insert():
    conn = MySQLdb.connect(user = 'user001', password = '12345', host = '127.0.0.1', db = 'my_db', charset = 'utf8')
    cursor = conn.cursor()

    sql = "insert into accounts(id) values (1)"
    cursor.execute(sql)

    conn.commit()
    cursor.close()
    conn.close()

if __name__ == "__main__":
    Select()
    Insert()
実行結果
$ python3 a.py
123


Windows/Linuxでポートフォワーディングする方法

Ⅰ. はじめに

タイトルの通り「Windows/Linuxでポートフォワーディングする方法」です。

Ⅱ. やり方(Windows

この記事ではnetshコマンドを利用する方法を紹介します。

127.0.0.1:8080に来たパケットを全て127.0.0.1:80に流す」設定を追加する
netsh interface portproxy add v4tov4 listenport=8080 listenaddr=127.0.0.1 connectport=80 connectaddress=127.0.0.1
設定を削除する
netsh interface portproxy delete v4tov4 listenport=8080 listenaddr=127.0.0.1
設定を確認する
netsh interface portproxy show all
留意点

うまく動作しない場合はWindowsファイアウォールの設定を見直す必要があります。

その他

192.168.0.100へのパケットを全てlocalhostに流す

netsh int ip add address "Loopback" 192.168.0.100

Ⅲ. やり方(Linux

この記事ではredir(uredir)を利用する方法を紹介します。

プロトコル コマンド名
TCP redir
UDP uredir

iptablesufwコマンドを利用する方法もあります。

「0.0.0.0:8080でリッスンし、パケットを全て10.0.0.2:8081に流す」設定を追加する
redir -n 0.0.0.0:8080 10.0.0.2:8081

C#でenumに任意の文字列(string)を設定する方法

Ⅰ. はじめに

タイトルの通り「C#enumに任意の文字列(string)を設定する方法」です。

Ⅱ. サンプルプログラム1

Install-Package FastEnum

Program.cs

enum Company
{
  [EnumMember(Value = "Apple, Inc.")]
  Apple = 0,
}

class Program
{
  static void Main()
  {
    Company.Apple.GetEnumMemberValue();
  }
}

実行結果

省略

Ⅲ. サンプルプログラム2

EnumExtensions.cs

public static class EnumExtensions
{
  public static string GetDescription(this Enum value)
  {
    var field = value.GetType().GetField(value.ToString());
    var attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
    if (attribute != null)
    {
      return attribute.Description;
    }
    else
    {
      return value.ToString();
    }
  }
}

Program.cs

class Program
{
  enum Prefecture
  {
    [Description("北海道")]
    Hokkaido,

    [Description("青森")]
    Aomori,

    [Description("岩手")]
    Iwate
  }

  static void Main(string[] args)
  {
    var prefecture = Prefecture.Hokkaido;

    Console.WriteLine(prefecture);
    Console.WriteLine(prefecture.GetDescription());
  }
}

実行結果


C#でGooglePlayStoreのAPIを呼び出す方法

Ⅰ. はじめに

タイトルの通り「C#でGooglePlayStoreのAPIを呼び出す方法」です。

Ⅱ. サンプルプログラム

予め以下のパッケージをNuGetからインストールして下さい。

Install-Package GooglePlayStoreApi
検索する
var email = "abc@gmail.com";
var password = "mypassword";
var androidId = Guid.NewGuid().ToString("N").Substring(0, 16);

var client = new GooglePlayStoreClient(email, password, androidId);
var token = await client.GetGoogleToken();
var auth = await client.GetGoogleAuth(token);

var searchResult = await client.Search("gmail");
foreach (var appDetail in searchResult.PreFetch[0].Response.Payload.ListResponse.Doc[0].Child.Select(x => x.Child[0]))
{
  var appId = appDetail.Docid;
  var appName = appDetail.Title;

  Console.WriteLine($"{appId},{appName}");
}
アプリの詳細情報を取得する
var email = "abc@gmail.com";
var password = "mypassword";
var androidId = Guid.NewGuid().ToString("N").Substring(0, 16);

var client = new GooglePlayStoreClient(email, password, androidId);
var token = await client.GetGoogleToken();
var auth = await client.GetGoogleAuth(token);

var appDetail = await client.AppDetail("com.google.android.gm");
var appName = appDetail.DocV2.Title;
var descriptionHtml = appDetail.DocV2.DescriptionHtml;
var versionCode = appDetail.DocV2.Details.AppDetails.VersionCode;
var versionString = appDetail.DocV2.Details.AppDetails.VersionString;
var permissions = appDetail.DocV2.Details.AppDetails.Permission;
var offerType = appDetail.DocV2.Offer[0].OfferType;
APKをダウンロードする
var email = "abc@gmail.com";
var password = "mypassword";
var androidId = Guid.NewGuid().ToString("N").Substring(0, 16);

var client = new GooglePlayStoreClient(email, password, androidId);
var token = await client.GetGoogleToken();
var auth = await client.GetGoogleAuth(token);

var bytes = await client.DownloadApk("com.google.android.gm");
File.WriteAllBytes("Gmail.apk", bytes);

Linuxで他プロセスのメモリを読み書きする方法

Ⅰ. はじめに

タイトルの通り「Linuxで他プロセスのメモリを読み書きする方法」です。
Windowsの場合はOpenProcessしてプロセスのハンドルを取得した後にReadProcessMemory, WriteProcessMemoryすればOKです。

Ⅱ. サンプルプログラム

メモリを読み書き「される」側(a.cpp)
#include <stdio.h>
#include <unistd.h>

int main(void) {
  auto x = 100;

  while(true) {
    printf("%p = %d\n", &x, x);
    sleep(1);
  }

  return 0;
}
メモリを読み書き「する」側(b.cpp)
#include <sys/uio.h>
#include <stdio.h>

int main(void) {
  pid_t pid = 397;
  int buffer;
  struct iovec local, remote;

  local.iov_base = &buffer;
  local.iov_len = sizeof(int);
  remote.iov_base = reinterpret_cast<void*>(0x7ffff09c81d4);
  remote.iov_len = sizeof(int);

  // 他プロセスのメモリを読む
  process_vm_readv(pid, &local, 1, &remote, 1, 0);
  printf("%d\n", buffer);

  // 他プロセスのメモリに書き込む
  buffer = 200;
  process_vm_writev(pid, &local, 1, &remote, 1, 0);

  // 他プロセスのメモリを読む(2回目)
  process_vm_readv(pid, &local, 1, &remote, 1, 0);
  printf("%d\n", buffer);

  return 0;
}

Ⅲ. 実行結果

f:id:kagasu:20180122233654p:plain