Created
September 11, 2014 13:01
-
-
Save MadFaill/9103ce2f2243387bfdcd to your computer and use it in GitHub Desktop.
Python + PHP Socket connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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