Recent

Author Topic: conversion from python to lazarus  (Read 6321 times)

id_ruben

  • Newbie
  • Posts: 4
conversion from python to lazarus
« on: July 04, 2016, 09:04:59 pm »
HI,

How conversion of python code to  lazarus code.
What components should I use.

python import:
import httplib
import urllib
import json
import hashlib
import hmac
import time
import threading
import urllib2


python code 1 :
  req = urllib2.Request(url=myurl)
  response = urllib2.urlopen(req)
  jsonString = response.read()   
  objFromJSON = json.loads(jsonString)




python code 2 :
    parms = {"param1":"vparam1","param2":"vparam2","param3":'vparam3"}
       parms=urllib.urlencode(parms)
   
      H = hmac.new(key2, digestmod=hashlib.sha512)
      H.update(parms)
     sign = H.hexdigest()
     headers = {"Content-type": "application/x-www-form-urlencoded","Key":key1,"Sign":sign}
     conn = httplib.HTTPSConnection(mysite2)
     conn.request("POST", "/tapi", parms, headers)
     response = conn.getresponse()
     resp_1= json.load(response)


* Lazarus 1.2.0
* Fedora 21 LXDE

Thanks you.

Ruben

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: conversion from python to lazarus
« Reply #1 on: July 04, 2016, 10:12:10 pm »
classes (for TThread, although I don't see where you use it in the python code), cthreads (for unix platforms), fphttpclient, fpjson. hash package has no hmac-sha512 implementation so that will require some 3rd party lib like flcHash or RHL.
Done, no components needed.

id_ruben

  • Newbie
  • Posts: 4
Re: conversion from python to lazarus
« Reply #2 on: July 07, 2016, 09:31:32 pm »
classes (for TThread, although I don't see where you use it in the python code), cthreads (for unix platforms), fphttpclient, fpjson. hash package has no hmac-sha512 implementation so that will require some 3rd party lib like flcHash or RHL.
Done, no components needed.

python            lazarus       I am not sure if this is appropriate
threading           ttimer


python
  req = urllib2.Request(url=myurl)
  response = urllib2.urlopen(req)

Lazarus
LSHTTPGetText(myurl, strbuf)

but
uses fpjson, jsonparser;
error in GetJSON
Identifier Not Found "GetJSON"


while I use " findpart " to retrieve information from "strbuf" .

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: conversion from python to lazarus
« Reply #3 on: July 08, 2016, 10:55:20 am »
python            lazarus       I am not sure if this is appropriate
threading           ttimer
Well, TTimer runs on its own thread. But if the code is meant for threading, then use TThread, not TTimer.
python
  req = urllib2.Request(url=myurl)
  response = urllib2.urlopen(req)

Lazarus
LSHTTPGetText(myurl, strbuf)

but
uses fpjson, jsonparser;
error in GetJSON
Identifier Not Found "GetJSON"


while I use " findpart " to retrieve information from "strbuf" .
Update your FPC (and Lazarus) first. Lazarus 1.2.0 comes with FPC 2.6.2 and is more than 2 years old. GetJSON should be in fpjson unit.

id_ruben

  • Newbie
  • Posts: 4
Re: conversion from python to lazarus
« Reply #4 on: July 08, 2016, 10:34:03 pm »
python            lazarus       I am not sure if this is appropriate
threading           ttimer
Well, TTimer runs on its own thread. But if the code is meant for threading, then use TThread, not TTimer.
python
  req = urllib2.Request(url=myurl)
  response = urllib2.urlopen(req)

Lazarus
LSHTTPGetText(myurl, strbuf)

but
uses fpjson, jsonparser;
error in GetJSON
Identifier Not Found "GetJSON"


while I use " findpart " to retrieve information from "strbuf" .
Update your FPC (and Lazarus) first. Lazarus 1.2.0 comes with FPC 2.6.2 and is more than 2 years old. GetJSON should be in fpjson unit.

install lazarus 1.6
fpc-3.0.0-1.i686.rpm
fpc-src-3.0.0-151202.i686.rpm
lazarus-1.6-0.i686.rpm


simple program

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs;

type
  TForm1 = class(TForm)
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

end.

               

Error :
Compile Project, Target: /var/tmp/project1: Exit code 256, Errors: 1
lazutf8.pas(184,3) Fatal: Cannot find gettext used by LazUTF8 of package LazUtils.

but if using the root , succeed
Compile Project, Target: /root/tmp/project1: Success

Directory FPC and FPC-src deleted  replaced with a new one.

how to use Lazarus without root  ?
Solved
Compile Project, Target: /home/ruben/tmp/project1: Success
« Last Edit: July 09, 2016, 12:09:56 am by id_ruben »

 

TinyPortal © 2005-2018