Raised This Month: $301 Target: $400
 75% 

Communication between AMXX and Python using sockets


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 12-12-2020 , 02:55   Communication between AMXX and Python using sockets
Reply With Quote #1

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);     } }
__________________
Relaxing is offline
Scrooge2029
Junior Member
Join Date: Dec 2020
Old 12-29-2020 , 05:35   Re: Communication between AMXX and Python using sockets
Reply With Quote #2

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?

Last edited by Scrooge2029; 12-29-2020 at 20:58. Reason: Issue
Scrooge2029 is offline
Relaxing
AlliedModders Donor
Join Date: Jun 2016
Location: White Plains
Old 02-01-2021 , 02:46   Re: Communication between AMXX and Python using sockets
Reply With Quote #3

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.
__________________
Relaxing is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:33.


Powered by vBulletin®
Copyright ©2000 - 2021, vBulletin Solutions, Inc.
Theme made by Freecode