Sending a message from PHP to Python through a socket

后端 未结 1 429
醉话见心
醉话见心 2021-01-07 02:49

I\'m trying to send a message to a Python socket using PHP and that it prints the message.

Here is the PHP code so far:



        
相关标签:
1条回答
  • 2021-01-07 03:43

    Try the following:

    import socket
    
    s = socket.socket()
    host = "localhost"
    port = 12345
    s.bind((host, port))
    
    s.listen(5)
    while True:
       c, addr = s.accept()
       data = c.recv(1024)
       if data: print data
       c.close()
    

    The main problem is your code was calling s.recv() when it should have been on c.recv(). Also make sure you check the data received (is it None?) before printing.

    0 讨论(0)
提交回复
热议问题