Perhatikan bahwa jika terjadi kesalahan selama pembuatan soket, maka soket. kesalahan dilemparkan dan kami hanya dapat terhubung ke server dengan mengetahui IP-nya. Anda dapat menemukan IP server dengan menggunakan ini. Show $ ping www.google.com Anda juga dapat menemukan IP menggunakan python. import socket ip = socket.gethostbyname('www.google.com') print ip_ Berikut adalah contoh skrip untuk menghubungkan ke Google Python3
$ ping www.google.com0 $ ping www.google.com1 $ ping www.google.com2 $ ping www.google.com3 $ ping www.google.com1 $ ping www.google.com5
$ ping www.google.com6 $ ping www.google.com7 $ ping www.google.com8 $ ping www.google.com9 import socket ip = socket.gethostbyname('www.google.com') print ip0 import socket ip = socket.gethostbyname('www.google.com') print ip1 $ ping www.google.com8 import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 import socket ip = socket.gethostbyname('www.google.com') print ip5 import socket ip = socket.gethostbyname('www.google.com') print ip6 import socket ip = socket.gethostbyname('www.google.com') print ip7 import socket ip = socket.gethostbyname('www.google.com') print ip8 $ ping www.google.com8 import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 Socket successfully created the socket has successfully connected to google on port == 173.194.40.192 Socket successfully created the socket has successfully connected to google on port == 173.194.40.193 Socket successfully created the socket has successfully connected to google on port == 173.194.40.194
Socket successfully created the socket has successfully connected to google on port == 173.194.40.195 ________24______6 import socket ip = socket.gethostbyname('www.google.com') print ip0 Socket successfully created the socket has successfully connected to google on port == 173.194.40.198
$ ping www.google.com6 $ ping www.google.com7 $ ping www.google.com8 # start the server $ python server.py # keep the above terminal open # now open another terminal and type: $ telnet localhost 123452 import socket ip = socket.gethostbyname('www.google.com') print ip0 # start the server $ python server.py # keep the above terminal open # now open another terminal and type: $ telnet localhost 123454 # start the server $ python server.py # keep the above terminal open # now open another terminal and type: $ telnet localhost 123455 import socket ip = socket.gethostbyname('www.google.com') print ip6 import socket ip = socket.gethostbyname('www.google.com') print ip7 # start the server $ python server.py # keep the above terminal open # now open another terminal and type: $ telnet localhost 123458
$ ping www.google.com8 # in the server.py terminal you will see # this output: Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)0 $ ping www.google.com8 import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 # in the server.py terminal you will see # this output: Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)4 import socket ip = socket.gethostbyname('www.google.com') print ip6 $ ping www.google.com8 # in the server.py terminal you will see # this output: Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)7
# in the server.py terminal you will see # this output: Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)8 # in the server.py terminal you will see # this output: Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)_9
import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 # In the telnet terminal you will get this: Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Thank you for connectingConnection closed by foreign host.2 import socket ip = socket.gethostbyname('www.google.com') print ip6 Keluaran. Socket successfully created the socket has successfully connected to google on port == 173.194.40.19
Program server-klien sederhana.Server. Server memiliki metode bind() yang mengikatnya ke IP dan port tertentu sehingga dapat mendengarkan permintaan masuk pada IP dan port tersebut. Server memiliki metode listen() yang menempatkan server ke mode mendengarkan. Hal ini memungkinkan server untuk mendengarkan koneksi masuk. Dan terakhir server memiliki metode accept() dan close(). Metode accept memulai koneksi dengan klien dan metode close menutup koneksi dengan klien. Python3# In the telnet terminal you will get this: Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Thank you for connectingConnection closed by foreign host.4 $ ping www.google.com1 # In the telnet terminal you will get this: Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Thank you for connectingConnection closed by foreign host.6
# In the telnet terminal you will get this: Trying ::1... Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Thank you for connectingConnection closed by foreign host.7 $ ping www.google.com9 import socket ip = socket.gethostbyname('www.google.com') print ip0 # start the server: $ python server.py Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)0 import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 import socket ip = socket.gethostbyname('www.google.com') print ip5 import socket ip = socket.gethostbyname('www.google.com') print ip6
# start the server: $ python server.py Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)5 # start the server: $ python server.py Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)6 Socket successfully created the socket has successfully connected to google on port == 173.194.40.196 import socket ip = socket.gethostbyname('www.google.com') print ip0 # start the server: $ python server.py Socket successfully created socket binded to 12345 socket is listening Got connection from ('127.0.0.1', 52617)9
# start the client: $ python client.py Thank you for connecting0 # start the client: $ python client.py Thank you for connecting_1 # start the client: $ python client.py Thank you for connecting_2 # start the client: $ python client.py Thank you for connecting_3 # start the client: $ python client.py Thank you for connecting_4 # start the client: $ python client.py Thank you for connecting_5 import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 # start the client: $ python client.py Thank you for connecting8 Socket successfully created the socket has successfully connected to google on port == 173.194.40.193 # An example script to connect to Google using socket 0
________115 $ ping www.google.com_115_______3 # An example script to connect to Google using socket 4import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 # An example script to connect to Google using socket 7# An example script to connect to Google using socket 8
$ ping www.google.com_00 $ ping www.google.com01 $ ping www.google.com02 $ ping www.google.com7
$ ping www.google.com_04 $ ping www.google.com05 $ ping www.google.com06 import socket ip = socket.gethostbyname('www.google.com') print ip0 $ ping www.google.com08 $ ping www.google.com05 import socket ip = socket.gethostbyname('www.google.com') print ip3 import socket ip = socket.gethostbyname('www.google.com') print ip4 $ ping www.google.com12 $ ping www.google.com13
$ ping www.google.com05 $ ping www.google.com15 $ ping www.google.com05 $ ping www.google.com17 $ ping www.google.com18 $ ping www.google.com19
$ ping www.google.com05 $ ping www.google.com21 $ ping www.google.com05 $ ping www.google.com23 $ ping www.google.com_05 $ ping www.google.com05 $ ping www.google.com26 $ ping www.google.com05 $ ping www.google.com28
Klien. # start the server $ python server.py # keep the above terminal open # now open another terminal and type: $ telnet localhost 12345 Jika 'telnet' tidak dikenali. Di windows cari fitur windows dan aktifkan fitur "klien telnet". Bagaimana cara menghubungkan klien ke server menggunakan soket di python?Untuk menggunakan koneksi soket python, kita perlu mengimpor modul soket. Kemudian, secara berurutan kita perlu melakukan beberapa tugas untuk membuat koneksi antara server dan klien . Kita bisa mendapatkan alamat host dengan menggunakan socket. fungsi gethostname().
Bagaimana cara pilih pilih bekerja di python?Fungsi select() Python adalah antarmuka langsung ke implementasi sistem operasi yang mendasarinya. Ini memonitor soket, file terbuka, dan pipa (apa pun dengan metode fileno() yang mengembalikan deskriptor file yang valid) hingga menjadi dapat dibaca atau ditulis, atau terjadi kesalahan komunikasi.
Bagaimana Anda menentukan soket TCP dengan python?Dimulai dengan membuat soket TCP/IP. . impor soket impor sys # Buat soket TCP/IP sock = soket. soket (soket. . # Ikat soket ke port server_address = ('localhost', 10000) print >>sys. stderr, 'memulai pada %s port %s' % server_address sock. . # Dengarkan kaus kaki koneksi yang masuk Bagaimana Anda membuat soket klien dengan python?Klien soket sederhana Python . membuat soket dapatkan alamat ip server dari nama domain terhubung ke server menggunakan alamat ip kirim permintaan ke server menerima data (halaman web) Dalam kode yang akan terlihat seperti. # Contoh klien soket dengan python. soket impor. impor sistem. tuan rumah = 'www. bahasa pemrograman python. com' port = 80 # web |