Hello all, I'm trying to run a simple client/server socket progaram from a refrence of the net to get my feet wet a bit with network programming. I can connect fine but can't seem to send data from the client due to the fact python is saying its of type"str" and not of type "string". I searched google and the forum for an answer but can't seem to find anything. Here is the code and error I'm getting.

#simple illustration client/server pair; client program sends a string
#to server, which echoes it back to the client (in multiple copies),
#and the latter prints to the screen

#this is the client

import socket
import sys

#create a socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#connect to server
host = ""    #server address
port = 2345           #server listening port
s.connect((host, port))
data = "stuff"
s.send(data) #send test string to server

#read echo

i = 0
    data = s.recv(1000000)  #read upto 1000000 bytes
    i += 1
    if (i < 5):   #look at only first part of message
    if not data:  #if end data, leave loop
    print("recieved", len(data), "bytes")

#close the connection

And the error is this.

Traceback (most recent call last):
  File "C:\Users\Nancy\Desktop\Desktop\wills stuff\programs\network\testclient.py", line 18, in <module>
    s.send(data) #send test string to server
TypeError: send() argument 1 must be string or buffer, not str
7 Years
Discussion Span
Last Post by willygstyle

Ok so upon further searching I found that in order for this to work you have to make transfers in binary, and then decode on the recieving end. Like this...

#client sending
s.send(bytearray(data, 'utf-8'))
#server recieving
data = conn.recv(1000000).decode('utf-8')

But now I'm really stumped on how to get a makefile transfer of data to work, I think I have tried about everything under the sun to get it to print out on the client-side but the program crashes whenever it gets to that point, I'm pretty sure the transfer goes well, but not 100% on that either this is what I've got at the moment.

#Server for remote version of dir command in windows

#user can check load on machine without logging in or even
#having an account on the remote machine


#set port number for cleint to connect to

import socket, sys, os

def main():
    ls = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    port = 9999
    ls.bind(('', port))

    while (1):
        (conn, addr) = ls.accept() #get client info
        print("client is at", addr)
        #get dir command from client
        rc = conn.recv(3).decode('utf-8')
        print("got rc")
        #run the command from server in Unix_sytle pipe
        ld = os.popen(rc) #directory contents in ld
        print("running command")
        #create a file_like object from the connection socket
        flo = conn.makefile('b', 0) #write only, unbuffered
        print("making connecter")
        #write lines to the network
        print("sent info")
        #clean up
        #must close both the socket and the wrapper
        print("closing connection")
        print("connection closed")

if __name__ == '__main__':

#client for a server with remote capabilities

#user can check load on machine without logging in (or even without
#having an account on the remote machine)


#set remotehost and port

import socket, sys

def main():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = ""
    port = 9999

    #connect to remote host
    s.connect((host, port))
    #send dir command to host
    s.send(bytearray("dir", 'utf-8'))
    print("sending command")

    #create "file-like object" flo
    flo = s.makefile('b', 0)    #read only, unbuffered
    print("reading file")
    #now can call readlines() on flo, and also use the fact that
    #stdout is a file-like object too.
    print("got", flo)
    print(flo.read())  #also tried the sys.stdout.writelines
    #(flo.readlines()) from example also tried .decode()

if __name__ == '__main__':

Any suggestion or help appretiated.


Beggining to believe this is a bug. The only documentation I can find said that a bug related to this was fixed but I still get hung up whenver I try to read from the object. This is the output I get when I do some checks on the object itself.

#preform check on IO Object
print(flo.readlines, flo.isatty(), flo.readable())

<bound method SocketIO.readlines of <socket.SocketIO object at 0x034E92B0>> False True

Anybody know if this a bug or why it hangs on the client if I try to read from it?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.