Python< - > C通过插座 [英] Python <-> C via sockets
问题描述
我可能错过了一些相当明显的东西,但是有一个已知的问题是让基于Python的套接字程序与基于C的套接字程序进行通信吗?一个用Python编写的简单回显服务器
(实际上来自Python中的一个例子)将很高兴地向基于Python的客户端讨论
。如果基于C的客户端被替换连接
被拒绝(或者C客户端报告)。 C客户端将与
a C服务器通话。
-
Nick Keighley
I''m probably missing something rather obvious, but is there a known
problem with getting a Python based socket program to communicate with
a C based socket program? A simple echo server written in Python
(the example from Python in a Nutshell actually) will happily talk
to a Python based client. If a C based client is substitued the connection
is refused (or so the C client reports). The C client will talk to
a C server.
--
Nick Keighley
推荐答案
Nick Keighley:
Nick Keighley:
获取基于Python的套接字程序与基于C的套接字程序进行通信是否存在已知问题?
is there a known problem with getting a Python based socket program to
communicate with a C based socket program?
编号客户端和服务器是完全独立的,可以用
任何语言实现。
他们必须说出来当然,相同的应用程序级协议,
TCP / IP。
-
$ b $bRenéPijlman
No. Client and server are completely independent and can be implemented in
any language.
They must speak the same application level protocol of course, on top of
TCP/IP.
--
René Pijlman
Nick Keighley写道:
Nick Keighley wrote:
我可能错过了一些相当明显的东西,但是获得基于Python的套接字程序有一个已知的问题与基于C的套接字程序进行通信?用Python编写的一个简单的echo服务器(实际上来自Python的一个例子)将很高兴地与基于Python的客户端交谈。如果基于C的客户端被替换,则连接被拒绝(或者C客户端报告)。 C客户端将与一个C服务器通信。
I''m probably missing something rather obvious, but is there a known
problem with getting a Python based socket program to communicate with
a C based socket program? A simple echo server written in Python
(the example from Python in a Nutshell actually) will happily talk
to a Python based client. If a C based client is substitued the connection
is refused (or so the C client reports). The C client will talk to
a C server.
通过套接字确定C和Python之间的通信工作:我们每个人都这样做
天(不开玩笑;-)
你能发一个你的代码示例吗?
-
- Eric Brunel< eric dot brunel at pragmadev dot com> -
PragmaDev:实时软件开发工具 - http://www.pragmadev .com
Communications between C and Python via sockets definetly work: we do that every
day (no kidding ;-)
Can you post an example of your code?
--
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
Eric Brunel< er ********* @ pragmadev.N0SP4M.com>在消息新闻中写道:< bk ********** @ news-reader1.wanadoo.fr> ...
Eric Brunel <er*********@pragmadev.N0SP4M.com> wrote in message news:<bk**********@news-reader1.wanadoo.fr>...
Nick Keighley写道:
Nick Keighley wrote:
我可能错过了一些相当明显的东西,但是有一个已知的问题是让基于Python的套接字程序与
进行通信一个基于C的套接字程序?用Python编写的一个简单的echo服务器(实际上来自Python的一个例子)将很高兴地与基于Python的客户端交谈。如果基于C的客户端被替换,则连接被拒绝(或者C客户端报告)。 C客户端将与一个C服务器通信。
I''m probably missing something rather obvious, but is there a known
problem with getting a Python based socket program to communicate with
a C based socket program? A simple echo server written in Python
(the example from Python in a Nutshell actually) will happily talk
to a Python based client. If a C based client is substitued the connection
is refused (or so the C client reports). The C client will talk to
a C server.
C和Python之间的通信通过套接字确定工作:我们每天都这样做(不开玩笑; - 你可以发一个你的代码示例吗?
Communications between C and Python via sockets definetly work: we do that
every day (no kidding ;-)
Can you post an example of your code?
我认为套接字实际工作我猜的是轻微的
接口的差异导致我将两端设置为
稍微
不同。
Python Echo Server
------------------
导入套接字
DEFAULT_PROTOCOL = 0
PORT = 8702
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM,
DEFAULT_PROTOCOL)>
sock.bind(('''',PORT))
sock.listen(5)
#等待连接
尝试:
而True:
newSocket,address = sock.accept()
print" connected from ;,地址
而True:
receivedData = newSocket.recv(8192)
如果没有收到数据:
休息
打印"收到:",receivedData
newSocket.sendall(receivedData)
newSocket.close()
打印与...断开连接,地址
终于:
sock.close()
-------------------------------- -------------------------------------
C echo客户端(包括省略)
-------------------------------
#定义DEFAULT_PROTOCOL 0
#define PORT 8702
#define HOSTlocalhost
/ * #define HOST" 127.0.0.1" * /
/ * #define HOST" cmopc018" * /
#define TYPE SOCK_STREAM
int main(无效)
{
struct hostent * phe;
struct sockaddr_in sin;
int s;
s = socket(PF_INET,TYPE,DEFAULT_PROTOCOL);
if(s< 0)
raise_report(LEVEL_FATAL," tiny_echo",不能创建套接字:
%s",strerror(errno) ));
/ *
*连接插座
* /
memset( & sin,0,sizeof sin);
sin.sin_family = AF_INET;
sin.sin_port = PORT;
if((phe = gethostbyname(HOST)))
{
memcpy(& sin.sin_addr,phe-> h_addr_list [0],phe-> h_length);
raise_report(LEVEL_INFO," tiny_echo"," addr is%X",
sin.sin_addr);
}
else
if((sin.sin_addr.s_addr = inet_addr(HOST))== INADDR_NONE)
raise_report(LEVEL_FATAL," tiny_echo"," can) '' TG et \%s \>
主机条目,主机;
if(connect(s,(struct sockaddr *)& ; sin,sinof sin)< 0)
raise_report(LEVEL_FATAL," tiny_echo",无法连接到
%s。%d:%s",HOST,sin.sin_port ,strerror(errno));
raise_report(LEVEL_INFO," tiny_echo"," CONNECTED");
返回0;
}
---------------------------------- --------------------------------------------
-
Nick Keighley
well I assumed sockets actually worked I was guessing the slight
differences in the interfaces caused me to set the two ends up
slightly
differently.
Python Echo Server
------------------
import socket
DEFAULT_PROTOCOL = 0
PORT = 8702
sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM,
DEFAULT_PROTOCOL)
sock.bind (('''', PORT))
sock.listen (5)
# wait for a connection
try:
while True:
newSocket, address = sock.accept ()
print "connected from", address
while True:
receivedData = newSocket.recv (8192)
if not receivedData:
break
print "received: ", receivedData
newSocket.sendall (receivedData)
newSocket.close()
print "disconnected from", address
finally:
sock.close ()
---------------------------------------------------------------------
C echo client (includes omitted)
-------------------------------
#define DEFAULT_PROTOCOL 0
#define PORT 8702
#define HOST "localhost"
/* #define HOST "127.0.0.1" */
/* #define HOST "cmopc018" */
#define TYPE SOCK_STREAM
int main (void)
{
struct hostent *phe;
struct sockaddr_in sin;
int s;
s = socket (PF_INET, TYPE, DEFAULT_PROTOCOL);
if (s < 0)
raise_report (LEVEL_FATAL, "tiny_echo", "can''t create socket:
%s", strerror (errno));
/*
* connect to the socket
*/
memset (&sin, 0, sizeof sin);
sin.sin_family = AF_INET;
sin.sin_port = PORT;
if ((phe = gethostbyname (HOST)))
{
memcpy (&sin.sin_addr, phe->h_addr_list[0], phe->h_length);
raise_report (LEVEL_INFO, "tiny_echo", "addr is %X",
sin.sin_addr);
}
else
if ((sin.sin_addr.s_addr = inet_addr (HOST)) == INADDR_NONE)
raise_report (LEVEL_FATAL, "tiny_echo", "can''t get \"%s\"
host entry", HOST);
if (connect (s, (struct sockaddr *)&sin, sizeof sin) < 0)
raise_report (LEVEL_FATAL, "tiny_echo", "can''t connect to
%s.%d: %s", HOST, sin.sin_port, strerror (errno));
raise_report (LEVEL_INFO, "tiny_echo", "CONNECTED");
return 0;
}
------------------------------------------------------------------------------
--
Nick Keighley
这篇关于Python< - > C通过插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!