مشکل این کد چیه چره هر چی می نویسم داخل پرانتز ها type error میده؟

لطفا راهنمایی کنید هیچ کدوم (bool, int, str, float) رو پشتیبانی نمی کنه:

#!/usr/bin/python3.4



import socket

s = socket.socket()



host, port = socket.gethostname(), 12346

s.connect((host, port))
s.send('hello world')

اینم ارور:

C:\Python34\python.exe "C:/Users/mohammad/server and client/client.py"

Traceback (most recent call last):

  File "C:/Users/mohammad/server and client/client.py", line 14, in <module>

    s.send('hello world')

TypeError: 'str' does not support the buffer interface



Process finished with exit code 1

 

پاسخ ها

sokanacademy forum
کاربر سکان آکادمی 8 سال پیش

سلام متد bind هم لازم دارید

از این کد استفاده کنید

سمت سرور

# echo_server.py
import socket

host = ''        # Symbolic name meaning all available interfaces
port = 12345     # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
    data = conn.recv(1024)
    if not data: break
    conn.sendall(data)
conn.close()

 

سمت کلاینت

# echo_client.py
import socket

host = socket.gethostname()    
port = 12345                   # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.sendall(b'Hello, world')
data = s.recv(1024)
s.close()
print('Received', repr(data))

 

که  بدون مشکل در پایتون 3.5 اجرا می شود .

خروجی سرور

$ python server.py
Connected by ('151.232.116.124', 55734)

خروجی کلاینت

$ python client.py
Received b'Hello, world'

که ویدیو اجراhttps://www.youtube.com/watch?v=naPngYEFx_Y

 

online-support-icon