package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
)
type Client struct {
writer *bufio.Writer
}
func (client *Client) Write(data string) error {
fmt.Println("client write")
_, err := client.writer.WriteString(data)
err = client.writer.Flush()
return err
}
func NewClient(connection net.Conn) *Client {
writer := bufio.NewWriter(connection)
client := &Client{
writer: writer,
}
return client
}
type ChatRoom struct {
clients []*Client
}
func (chatRoom *ChatRoom) Broadcast(data string) {
deleted := 0
for i := range chatRoom.clients {
j := i - deleted
if err := chatRoom.clients[j].Write(data); err != nil {
fmt.Println(err, "deleted error")
chatRoom.clients = chatRoom.clients[:j+copy(chatRoom.clients[j:], chatRoom.clients[j+1:])]
deleted++
}
}
}
func (chatRoom *ChatRoom) Join(connection net.Conn) {
client := NewClient(connection)
chatRoom.clients = append(chatRoom.clients, client)
}
func NewChatRoom() *ChatRoom {
chatRoom := &ChatRoom{
clients: make([]*Client, 0),
}
return chatRoom
}
func main() {
chatRoom := NewChatRoom()
listener, _ := net.Listen("tcp4", ":3333")
go readAndSend(chatRoom)
for {
conn, err := listener.Accept()
if err != nil {
continue
}
chatRoom.Join(conn)
}
}
func readAndSend(chatRoom *ChatRoom) {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("Enter text: ")
text, _ := reader.ReadString('\n')
log.Print(text)
chatRoom.Broadcast(text)
}
}
import urllib
import urllib2, json, os.path
class Api():
main_url = 'https://api.vk.com/method/'
def method(self, method, params=None):
if not params:
params = {}
full_url = self.generate_url(method, params)
return urllib2.urlopen(full_url, timeout=timeout).read()
def generate_url(self, method, params):
m = self.main_url + method + "?" +urllib.urlencode(params)
return m
api = Api()
res = api.method('users.get', {"user_ids" : 1, 'fields': "photo_50,city,verified"})
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"os"
"strconv"
"strings"
)
var messages chan string
func main() {
messages = make(chan string, 10)
var port int = 3333
listen, err := net.Listen("tcp4", ":"+strconv.Itoa(port))
defer listen.Close()
if err != nil {
log.Fatalf("Прослушивание порта %d не удалось,\r\n\r\n %s", port, err)
os.Exit(1)
}
go func() {
log.Printf("Сервер слушает порт: %d", port)
reader := bufio.NewReader(os.Stdin)
for {
fmt.Print("Enter text: ")
text, _ := reader.ReadString('\n')
log.Print(text)
messages <- text
}
}()
for {
conn, err := listen.Accept()
if err != nil {
log.Fatalln(err)
continue
}
go handler(conn)
}
}
func handler(conn net.Conn) {
defer conn.Close()
var (
buf = make([]byte, 1024)
r = bufio.NewReader(conn)
w = bufio.NewWriter(conn)
)
ILOOP:
for {
n, err := r.Read(buf)
data := string(buf[:n])
switch err {
case io.EOF:
break ILOOP
case nil:
log.Println("Получил:", data)
if isTransportOver(data) {
break ILOOP
}
default:
log.Fatalf("Ошибка при получении:%s", err)
return
}
}
//данные из канала
msg := <-messages
w.Write([]byte(msg))
w.Flush()
}
func isTransportOver(data string) (over bool) {
over = strings.HasSuffix(data, "\r\n\r\n")
return
}
Обратите внимание — даже при использовании параметра offset для получения информации доступны только первые 1000 результатов.
class S():
kk = 0
def m(self):
kk+=1
return self
def p(self):
print(self.kk)
return self
s = S().m().m().m().p().m().m().p()