Search code examples
pythongodotgdscript

Connect python local server with godot


I'm trying to send data to a python server:

import socket

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('127.0.0.1',6000))

s.listen(5)

while True:
    clientsocket,address = s.accept()
    print(f"Got connection from {address} !")

from godot:

var socket = PacketPeerUDP.new()
socket.set_dest_address("127.0.0.1",6000)
socket.put_packet("quit".to_ascii())

based on this link

but it doesn't seem to be working, How do I send the data?


Solution

  • i'm not that familiar with python servers, but it looks like you have a python server that listens for TCP connections but in godot you connect via UDP Client.

    As seen in this Answer SOCK_STREAM is for TCP Server and SOCK_DGRAM for UDP.

    I am not sure which of those you want to use. An example server for UDP would be:

    import socket
    
    s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
    s.bind(('127.0.0.1',6000))
    bufferSize  = 1024
    
    #s.listen(5)
    print("running")
    while True:
        bytesAddressPair = s.recvfrom(bufferSize)
        message = bytesAddressPair[0]
        clientMsg = "Message from Client:{}".format(message)
        print(clientMsg)
    

    I copied most of it from here : Sample UDP Server

    If you wanted to have a TCP Server you should alter the Godot part to use a TCP Client. See the official docs here