TheNavigator Posted November 3, 2012 Share Posted November 3, 2012 (edited) I've made an SSL connection between a web server and a CentOS server using python and php. The 2 server communicate perfectly, however. after the first reply from the client, which is the php, the client isn't able to send any other data. Here's the code. <?php echo "<h2>TCP/IP Connection</h2>\n"; $fp = fsockopen("ssl://unlicrea.zapto.org", 21098, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = "START"; fwrite($fp, $out); $in = fgets($fp, 128); echo $in.="<br />"; $out = "TEST"; fwrite($fp, $out); fclose($fp); } ?> For the server: #!/usr/bin/python # Server example import socket from OpenSSL import crypto, SSL context = SSL.Context(SSL.SSLv23_METHOD) context.use_privatekey_file('key') context.use_certificate_file('cert') # Establish a TCP/IP socket s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) s = SSL.Connection(context, s) # Bind to TCP port No. 17642 ... s.bind(("",21098)) # ... and listen for anyone to contact you # queueing up to five requests if you get a backlog s.listen(1) # Servers are "infinite" loops handling requests while True: # Wait for a connection connect, address = s.accept() # Typically fork at this point # Receive up to 1024 bytes resp = (connect.recv(65535)).strip() # And if the user has sent a "SHUTDOWN" # instruction, do so (ouch! just a demo) print resp if resp == "START": connect.send("AUTHENTICATION DETAILS REQUIRED") else : connect.send("Error") resp = (connect.recv(65535)).strip() print resp # And there could be a lot more here! # When done with a connection close it connect.close() print "done",address # And loop for / wait for another client The python thing is set to print any received data to the console. "START" is received successfully, however, "TEST" isn't. I received some errors at the PHP side. error_log file, here they are. [03-Nov-2012 04:02:11 UTC] PHP Warning: fsockopen() [<a href='function.fsockopen'>function.fsockopen</a>]: SSL: crypto enabling timeout in /home/unlicrea/public_html/Codezilla/sample.php on line 5[03-Nov-2012 04:02:11 UTC] PHP Warning: fsockopen() [<a href='function.fsockopen'>function.fsockopen</a>]: Failed to enable crypto in /home/unlicrea/public_html/Codezilla/sample.php on line 5 [03-Nov-2012 04:02:11 UTC] PHP Warning: fsockopen() [<a href='function.fsockopen'>function.fsockopen</a>]: unable to connect to ssl://unlicrea.zapto.org:21098 (Unknown error) in /home/unlicrea/public_html/Codezilla/sample.php on line 5 [03-Nov-2012 04:02:30 UTC] PHP Warning: fwrite() [<a href='function.fwrite'>function.fwrite</a>]: SSL operation failed with code 1. OpenSSL Error messages: error:140D00CF:SSL routines:SSL_write:protocol is shutdown in /home/unlicrea/public_html/Codezilla/sample.php on line 22 [03-Nov-2012 04:03:42 UTC] PHP Warning: fsockopen() [<a href='function.fsockopen'>function.fsockopen</a>]: unable to connect to ssl://unlicrea.zapto.org:21098 (Connection refused) in /home/unlicrea/public_html/Codezilla/sample.php on line 5 [03-Nov-2012 04:06:52 UTC] PHP Warning: fwrite() [<a href='function.fwrite'>function.fwrite</a>]: SSL operation failed with code 1. OpenSSL Error messages: error:140D00CF:SSL routines:SSL_write:protocol is shutdown in /home/unlicrea/public_html/Codezilla/sample.php on line 22 Any help is appreciated. Thanks. Edited November 3, 2012 by TheNavigator Quote Link to comment Share on other sites More sharing options...
TheNavigator Posted November 4, 2012 Author Share Posted November 4, 2012 (edited) I've contacted namecheap support regarding this. They gave some help. The server works perfectly with another client. There's something wrong with php. Moreover, resp = (connect.recv(65535)).strip() print resp # And there could be a lot more here! if I deleted that resp = (connect.recv(65535)).strip() , the second one, php script works fine Can someone help? Edited November 4, 2012 by TheNavigator Quote Link to comment Share on other sites More sharing options...
TheNavigator Posted November 5, 2012 Author Share Posted November 5, 2012 Any help?.... Quote Link to comment Share on other sites More sharing options...
kicken Posted November 5, 2012 Share Posted November 5, 2012 $in = fgets($fp, 128); That is going to block until you read either 128 bytes of data, or a new-line character. Since your server is not sending data which meets either of those conditions, your PHP script is going end up stuck there waiting indefinitely. The easiest fix is to just make sure your server meets one of those conditions, such as adding \r\n to the end of the data you send: connect.send("AUTHENTICATION DETAILS REQUIRED\r\n") Quote Link to comment Share on other sites More sharing options...
TheNavigator Posted November 5, 2012 Author Share Posted November 5, 2012 AT LAST! That worked Thanks a lot kicken! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.