Skip to content

Instantly share code, notes, and snippets.

@MadFaill
Created September 11, 2014 13:01
Show Gist options
  • Save MadFaill/9103ce2f2243387bfdcd to your computer and use it in GitHub Desktop.
Save MadFaill/9103ce2f2243387bfdcd to your computer and use it in GitHub Desktop.
Python + PHP Socket connection
<?php
$socket = fsockopen('localhost', 8888);
$greeteng = trim(fread($socket, 2048));
for ($i=0; $i<100; $i++)
{
$message = array('return'=>"Line: $i");
$bt = fwrite($socket, json_encode($message)."\r\n");
if (!$bt) {
print "Host is down!\r\n";
exit;
}
$result = fread($socket, 2048);
var_dump(trim($result));
usleep(700000);
}
from twisted.internet import protocol, reactor
from twisted.protocols.basic import LineReceiver
import json
class commander(LineReceiver):
def connectionMade(self):
self.sendLine("Connected")
def lineReceived(self, line):
try:
jstring = json.loads(line.decode('utf-8'))
if 'return' in jstring:
self.sendLine(jstring['return'].encode('utf-8'))
else:
raise BaseException(message="Wrong Format")
except BaseException as ex:
self.sendLine(ex.message)
self.connectionLost()
class Factory(protocol.Factory):
def buildProtocol(self, addr):
return commander()
#run
reactor.listenTCP(8888, Factory())
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment