【Develope a rabbitmq application with python4delphi】


【Develope a rabbitmq application with python4delphi】
You can download the vcl from
https://github.com/pyscripter/python4delphi

The VCL can support python 2.x to 3.x,only need you modify the *.inc file!
In my sample,I use a variant import *.py and execute it.

procedure TForm1.btnPublishClick(Sender: TObject);
var
a, main: variant;
folder, aMessage: string;
vJpegStream : TMemoryStream;
vStrStream: TStringStream;
vEncd: TIdEncoderMIME;
vS, vMSG_ID, vHeads: string;
begin
// importing an external module and using it
// first, extend the path with our current folder
_folder := ExtractFilePath(Application.ExeName);
if (Length(_folder) > 0) and (_folder[Length(_folder)] = '\') then
Delete(_folder, Length(_folder), 1);
if not Boolean(SysModule.path.Contains(_folder)) then
SysModule.path.insert(0, _folder);
// import the module
_main := Import('testPika');
Assert( VarIsPythonFunction(__main__.put_message));
Assert( VarIsPythonCallable(__main__.put_message));
Assert( VarIsPythonDict(__main__.vHeaders));

// call one of his functions
// initialize the operands
a := NewPythonDict;
Assert(VarIsPython(a));
Assert(VarIsPythonMapping(a));
Assert(VarIsPythonDict(a));



adsPic.Close;
adsPic.CommandText := 'Select Top ' + fTopCount + ' * from ' + fTable;
adsPic.Open;
while not adsPic.Eof do
begin

vStrStream := TStringStream.Create('');
vJpegStream := TMemoryStream.Create;
TBlobField(adsPic.FieldByName(fMessageBody)).SaveToStream(vJpegStream);

vMSG_ID := adsPic.FieldByName(fMessageId).AsString;
//TIdEncoderMIME.EncodeStream: Indy function to encode stream content as base64
//注意:用EncdDecd.EncodeStream 方法转成的base64串会每隔76个字符后面加写#$D#$A,后者#012;


//这句非常重要,否则vS为空
vJpegStream.Position := 0;

//用这个函数编码后的图片,接收解码后才会正常显示!!!
vS := TIdEncoderMIME.EncodeStream(vJpegStream, vJpegStream.Size);

vHeads := '''Year'':' + adsPic.FieldByName('pic_year').AsString +
',''Month'':' + adsPic.FieldByName('pic_month').AsString +
',''Day'':' + adsPic.FieldByName('pic_day').AsString + '';
edt1.Text := vHeads;
main.vHeaders.clear();

// There is a bug in D2010 in which Char('a') gets translated to integer parameter
a.SetItem( string('Year'), adsPic.FieldByName('pic_year').AsInteger );
a.SetItem( string('Month'), adsPic.FieldByName('pic_month').AsInteger );
a.SetItem( string('Day'), adsPic.FieldByName('pic_day').AsInteger );

main.vHeaders := a;


try

if main.put_message(vS, vMSG_ID) = True Then
Memo1.Lines.Add(DateTimeToStr(Now)+ ' MessageID:' + vMSG_ID + ' 投递成功!');

except on E: Exception do
Memo1.Lines.Add(E.Message);
end;

vJpegStream.Free;
vStrStream.Free;
adsPic.Next;

end;

adsPic.Close;



end;

Comments

  1. Awesome. I've been thinking about integrating Python4Delphi (or just the CLI) with RAD Server.

    ReplyDelete

Post a Comment