473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Extending & Embedding Python

Hi all! I've a problem with a C++ class that has to be included in a
python application. One way to do it is Extending and Embedding the
Python Interpreter
Now i have 2 questions
1) Is there a one-file version of this tutorial?
2) Is there anyone that can help me with this problem? The class is
attached.

Thanks all.
Marco

Attached files:
------------------------------------------------------------------------------------------------------
#ifndef _PACKETMANAGER_H_
#define _PACKETMANAGER_H_
#include "Cipher/Cipher.h"

#define PAYLOAD_LENGHT 5000

//the our defined packe over the UDP datagram
struct packet {
int lenght; // sizeof(payload) = sizeof(int) + strlen(payload)
unsigned char payload[PAYLOAD_LENGHT]; // the xml string
} __attribute__ ((packed));
//! manage the send and receive of udp packet
/*! by default: send message non-cripted */
class PacketManager
{
private:
struct packet packet_to_send;
Cipher * pc;
int s; //socket descriptor
public:
PacketManager();
virtual ~PacketManager();

///Network member function
bool send(char * IPdest , int port_to);
unsigned char * receive (char * IPdest, int timeout); //non-blocking
unsigned char * waitMessage(char * IPfrom); //blocking

///Packet Builder member function
bool build_Packet();
bool setCipher(Cipher * c);
Cipher * getCipher();

///Set and Get function on all attributes

bool set_Payload(unsigned char * c, int lenght);
unsigned char * get_Payload();

bool set_packet_lenght(unsigned int lenght);
unsigned int get_packet_lenght();

void set_Socket(int socket);
int get_Socket();
bool buildPacket();
void setSocket(int socket);
packet getPacket();
bool setPacket(unsigned char * c,int lenght);
void closeSocket();
};

#endif //_PACKETMANAGER_H_
---------------------------------------------------------------------------------------------------
#include "Cipher/Cipher.h"
#include "Cipher/NullCipher.h"

#include "PacketManager.h"

#include "messages.h"

#define PORT_FROM 40004

//! manage the send and receive of udp packet
/*! by default: send message non-cripted */
PacketManager::PacketManager()
{

pc = new NullCipher();

s = create_UDP_socket(PORT_FROM);

}

PacketManager::~PacketManager()
{
close (s);
delete pc;
}

void PacketManager::setSocket(int socket)
{
s = socket;
}

bool PacketManager::buildPacket()
{
pc->encrypt(); //! ciphering
return true;

}

Cipher * PacketManager::getCipher()
{
return pc;
}

bool PacketManager::setCipher(Cipher * c)
{
pc=c;
return true;
}

//! send payload + header to IPdest at port_to
/*! send payload + header in an UDP packet
\param IPdest address to witch send the packet
\param port_to port to witch send the packet
*/
bool PacketManager::send(char *IPdest,int port_to)

{
//unsigned char * message_to_send = new unsigned
char(packet_to_send.lenght * sizeof(unsigned char));
unsigned char message_to_send[5000];

memcpy (message_to_send, &(packet_to_send.lenght) ,
sizeof(int));

unsigned char * tmp_payload = message_to_send;
tmp_payload+=sizeof(int);

memcpy(tmp_payload,packet_to_send.payload,strlen(( char*)packet_to_send.payload));

// memcpy (message_to_send + sizeof(int), packet_to_send.payload,
packet_to_send.lenght - sizeof(int)); //! mess =
[int][payload]

//memcpy (message_to_send, &packet_to_send ,sizeof (struct
packet)); //! mess = [packet_to_send]

packet_to_send.payload[packet_to_send.lenght-4]='\0';

printf ("Sto per inviare: \nlenght = \t%d \nPayload = \t%s\n",
packet_to_send.lenght , packet_to_send.payload);

message_to(s,port_to,IPdest, message_to_send , packet_to_send.lenght
);

//delete message_to_send;

//return 0;

return true;
}

//! wait for a packet, non blocking, don't check for IP
/*! wait for a packet, non blocking, don't check for IP
* param IPdest should be the IP from witch receive files
* param timeout should be the seconds to wait until a message was
received
*/
unsigned char * PacketManager::receive(char *IPdest, int timeout)
{
info risposta;
risposta = wait_message(s);
//!have to check if the message return from IPdest
return risposta.message;

}

//! wait for a packet from IPfrom, blocking
/*! wait for a packet from IPfrom, blocking, with check on IP
\param IPfrom the IP of the machine you want to wait for
*/
unsigned char * PacketManager::waitMessage(char *IPfrom)
{

info mess_received;
bool b = false;

while (b == false )
{
mess_received = wait_message(s);

in_addr_t ip_manager; //!check if the message return from IPdest
ip_manager = inet_addr(IPfrom);
unsigned char * ip_man = new unsigned char(4);
ip_man = (unsigned char * ) &ip_manager;
//check
if ( ip_man[0] == mess_received.ip[0] && \
ip_man[1] == mess_received.ip[1] && \
ip_man[2] == mess_received.ip[2] && \
ip_man[3] == mess_received.ip[3] \
)
b = true;

}

return mess_received.message;

}

bool PacketManager::setPacket(unsigned char * c,int lenght)
{
//strcpy ((char *) payload, (char *) c); //! with strcpy you
can't have \0 in middle of a payload, or it will be truncated

memcpy(packet_to_send.payload,c,lenght);
packet_to_send.lenght = lenght + sizeof (int);
return true;
}

packet PacketManager::getPacket()
{
return packet_to_send;
}

void PacketManager::closeSocket(){

close(s);
}
--------------------------------------------------------------------------------------------

Jan 12 '06 #1
3 2033
Marco Meoni schrieb:
Hi all! I've a problem with a C++ class that has to be included in a
python application. One way to do it is Extending and Embedding the
Python Interpreter
Now i have 2 questions
1) Is there a one-file version of this tutorial?
i'm not sure what tutorial you mean. are you talking about the
Extending and Embedding section of the python manual?

http://docs.python.org/ext/ext.html
2) Is there anyone that can help me with this problem? The class is
attached.


i haven't read your code, but you might want to take a look at SWIG:

http://www.swig.org/

--
David.
Jan 12 '06 #2
> i'm not sure what tutorial you mean. are you talking about the
Extending and Embedding section of the python manual?

http://docs.python.org/ext/ext.html


Yes, this section. Is there in one-file version? (pdf, ps, dvi, all in
1 html, etc...)

Jan 12 '06 #3
About SWIG:
This program has a lot of commands...
In your opinion what is the best settings for my use?
Thanks...
Marco

Jan 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mark Harrison | last post by:
Is the document Extending and Embedding the Python Interpreter http://www.python.org/doc/current/ext/ext.html easily available in any other form? Pointers to either PDF or a single web page...
4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
3
by: stefan | last post by:
Hi Folks, I currenty extended some of my C++ functionality to python and also embedded python to use python functionality in my C++ system (and use as well these extended functions). While...
1
by: Tommy Nordgren | last post by:
I want to write an application that embeds and extends (at least) the Python and Perl interpreters. Now i want to find as much as possible about the Python tools used for extending and embedding...
1
by: Richard Townsend | last post by:
In the "Extending and Embedding" part of the Python documentation: section 5.4 "Extending Embedded Python" - it describes how to use a Python extension module from Python that is embedded in a C...
1
by: jeremito | last post by:
I am trying to learn how to extend and/or embed Python. I have looked at the document "Extending and Embedding the Python Interpreter" and also "Python/C API Reference Manual. In the examples...
6
by: Qun Cao | last post by:
Hi Everyone, I am a beginner on cross language development. My problem at hand is to build a python interface for a C++ application built on top of a 3D game engine. The purpose of this python...
3
by: dmoore | last post by:
Hi Folks: I have a question about the use of static members in Python/C extensions. Take the simple example from the "Extending and Embedding the Python Interpreter" docs: A simple module...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.