带有PHP和Python的客户端服务器应用程序 [英] Client Server application with PHP and Python

查看:98
本文介绍了带有PHP和Python的客户端服务器应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从PHP客户端发送到Python服务器.

I am trying to send data from PHP Client to Python Server.

客户

<?php

$host = "127.0.0.1";
$port = 2000;

$output="datatatatatatta" ;

$socket1 = socket_create(AF_INET, SOCK_STREAM,0) or die("Could not create socket\n");

socket_connect ($socket1 , $host,$port ) ;

socket_write($socket1, $output, strlen ($output)) or die("Could not write output\n");

socket_close($socket1) ;


?>

服务器

import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

host= 'VAC01.VACLab.com'
port=int(2000)
s.bind((host,port))
s.listen(1)

conn,addr =s.accept()

print (conn,addr)

data=conn.recv(100000)
data=data.decode("utf-8")

s.close


FILE = open("c:/vinod/vin.txt","w")
FILE.write(str(data))
FILE.close()

在客户端,我收到以下错误消息.

On the client side I get the following error ,.

Warning: socket_connect() [function.socket-connect]: unable to connect [0]: No connection could be made because the target machine actively refused it. in C:\xampp\htdocs\xampp\socket.php on line 10

Warning: socket_write() [function.socket-write]: unable to write to socket [0]: A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied. in C:\xampp\htdocs\xampp\socket.php on line 12
Could not write output 

可以通过这种方式连接吗?

Is this possible to connect this way ?

推荐答案

您绑定到最有可能解析为127.0.0.1却连接到127.0.0.1的主机.那行不通.您有两种选择:

You bind to a host that most likely does not resolve to 127.0.0.1 but connect to 127.0.0.1. That cannot work. You have two options:

  1. 在两侧都使用127.0.0.1(或localhost)
  2. 在两侧使用主机名VAC01.VACLab.com
  1. Use 127.0.0.1 (or localhost) on both sides
  2. Use the hostname VAC01.VACLab.com on both sides

这篇关于带有PHP和Python的客户端服务器应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆