How to covert simple RPC Call in Python to Go?

429 views
Skip to first unread message

Christian LeMoussel

unread,
Oct 10, 2017, 2:49:26 PM10/10/17
to golang-nuts

I'm having trouble translating Python code into GO.


s = socks.socksocket()
s.connect((mining_ip_conf, int(port)))  # connect to pool
connections.send(s, "getwork", 10)
work_pack = connections.receive(s, 10)
db_block_hash = (work_pack[-1][0])
diff = int((work_pack[-1][1]))
paddress = (work_pack[-1][2])
netdiff = int((work_pack[-1][3]))
s.close()

From what I understand, this code queries an RPC server and retrieves a JSON RPC response.

I tried to do this in GO.


client, err := rpc.DialHTTP("tcp", fmt.Sprintf("%s:%d", mining_ip, port))
if err != nil {
    log.Fatalf("Error in dialing. %s", err)
}

var args interface{}
var result string
err = client.Call("getwork", args, &result)
if err != nil {
    log.Fatalf("getwork", err)
}

but I've got an this error.

2017/10/10 19:36:49 Error in dialing. dial-http tcp mining_ip:port: unexpected EOF

Does anyone see anything wrong with this GO code?

Shawn Milochik

unread,
Oct 10, 2017, 2:54:52 PM10/10/17
to golang-nuts
I think the problem is that the Python code is using a standard socket, and the Go code is using the rpc package. Try using net.Dial instead of rpc.DialHTTP.

A socket is just a socket. An rpc connection probably tries to do a bit more upon connection but isn't getting what it expects from the vanilla socket on the other side.


Dial returns a net.Conn, which has Read and Write methods which appear to be analogous to the send and receive methods used in the Python version.

Bruno Albuquerque

unread,
Oct 10, 2017, 2:55:47 PM10/10/17
to Christian LeMoussel, golang-nuts
Yes, the server is not an http/rpc server so using DialHTTP() does not make much sense. Try with net.DialTCP() and use Write() and Read() on the returned TCPConn to send requests and get the responses.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Gianguido Sorà

unread,
Oct 10, 2017, 3:56:59 PM10/10/17
to Bruno Albuquerque, Christian LeMoussel, golang-nuts
Since the server is communicating using JSON structures, you should unmarshal the Read() data in a struct to actually use it: https://blog.golang.org/json-and-go.

Unlike Python where JSON is typically handled through dictionaries, in Go you either provide a structure to unmarshal data to, or work your way through an  interface{} and do a lot of work using type assertion.

You can use tools like https://mholt.github.io/json-to-go/ to generate Go structs from arbitrary JSON data.

To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Gianguido (gsora) Sorà

Twitter: @gsora_
GitHub: @gsora

Christian LeMoussel

unread,
Oct 10, 2017, 4:51:24 PM10/10/17
to golang-nuts

connections
.send(s, "getwork", 10)

I searched a little bit more and here is in Python send() function

def send(sdef, data, slen):
    sdef.setblocking(0)

    sdef.sendall(str(len(str(json.dumps(data)))).encode("utf-8").zfill(slen))
    sdef.sendall(str(json.dumps(data)).encode("utf-8"))

Unfortunately, I don't know Python. the challenge is to rewrite the
send function in Go.




Justin Israel

unread,
Oct 11, 2017, 2:52:35 PM10/11/17
to Christian LeMoussel, golang-nuts
This code is converting the data to json twice.

The first time it converts it to get the length of the json string, and pad it out to the expected length (10).

The second send converts it again and just sends the encoded json string.

Your Go version would just want to encode your data as json, send and first send the length of it:

// Some Data
data := Data{"Foo"}
enc, err := json.Marshal(data)
if err != nil {
    panic(err)
}

size := fmt.Sprintf("%010d", len(data) )

// send size
// send enc





--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages