AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   Communication between AMXX and Python using sockets (https://forums.alliedmods.net/showthread.php?t=329181)

Relaxing 12-12-2020 02:55

Communication between AMXX and Python using sockets
 
Python script
Code:

import socket

host = ''
port = 54500

s = socket.socket()
s.bind((host, port))

s.listen(32)
conn, address = s.accept()
print("Connection from: " + str(address))

while True:
    data = conn.recv(256).decode()
    if not data:
        break
    print("from connected user: " + str(data))
    data = input(" -> ");
    conn.send(data.encode())

Pawn script
Code:
#include <amxmodx> #include <sockets> #define LOCAL "localhost" #define PORT 54500 new s, error, data[256]; public plugin_init(){     register_srvcmd("send", "srvcmd_send"); } public plugin_cfg(){     s = socket_open(LOCAL, PORT, SOCKET_TCP, error);         if (!error){         set_task(0.1, "get_data", .flags="b");         data = "connected";         socket_send(s, data, charsmax(data));           } } public srvcmd_send(){     new args[32];     read_args(args, charsmax(args));         socket_send(s, args, charsmax(args)); } public get_data(){     if (socket_is_readable(s)){         socket_recv(s, data, charsmax(data));         server_print(data);     } }

Scrooge2029 12-29-2020 05:35

Re: Communication between AMXX and Python using sockets
 
I need this!!! thx!
I can finally use TF to control BOTS, hhhhhh

2020.12.30
I just test the code, but it give me error code "3" when establishing connection...how could this be?
Err, it's a bit strange, when Py Listener start listening, netstat showed only 54500 of 0.0.0.0 is listening, while 127.0.0.1 doesnt.
Using both 127.0.0.1 or 0.0.0.0 on AMX Client failed, however, when I use my LAN IP (192.168.56.185) on AMX Client, it works.
For now I need to input my LAN IP manually, could it be more convenient? :oops:

Relaxing 02-01-2021 02:46

Re: Communication between AMXX and Python using sockets
 
I'm not sure if what you're doing is wrong either. Maybe that's how it works. Servers use the default 127.0.0.1 and desktop computers use their own ip which is assigned from the router since there could be serveral machines operating and localhost wouldn't be ideal in that case. You can port this to any computer languange that supports sockets, well mostly all of them.
One thing missing is that I rushed to post the script without making it so players can get specific results, such as showing their local wether a second or two later when they join the game.


All times are GMT -4. The time now is 19:42.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.